<!DOCTYPE html>



  


<html class="theme-next gemini use-motion" lang="zh-Hans">
<head><meta name="generator" content="Hexo 3.8.0">
  <meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<meta name="theme-color" content="#222">
<script>
    (function () {
        if ('') {
            if (prompt('请输入文章密码') !== '') {
                alert('密码错误！');
                if (history.length === 1) {
                    location.replace("https://google.com"); // 这里替换成你的首页
                } else {
                    history.back();
                }
            }
        }
    })();
</script>








<meta http-equiv="Cache-Control" content="no-transform">
<meta http-equiv="Cache-Control" content="no-siteapp">
















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css">







<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css">

<link href="/css/main.css?v=5.1.4" rel="stylesheet" type="text/css">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="Hexo, NexT">










<meta property="og:type" content="website">
<meta property="og:title" content="苏生不惑的博客">
<meta property="og:url" content="http://yoursite.com/page/39/index.html">
<meta property="og:site_name" content="苏生不惑的博客">
<meta property="og:locale" content="zh-Hans">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="苏生不惑的博客">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Gemini',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://yoursite.com/page/39/">



<meta name="referrer" content="never"> ​​​​


  <title>苏生不惑的博客</title>
  








</head>

<body itemscope="" itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left 
  page-home">
    <div class="headband"></div>

    <header id="header" class="header" itemscope="" itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">苏生不惑的博客</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br>
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/about/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br>
            
            关于
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br>
            
            归档
          </a>
        </li>
      

      
        <li class="menu-item menu-item-search">
          
            <a href="javascript:;" class="popup-trigger">
          
            
              <i class="menu-item-icon fa fa-search fa-fw"></i> <br>
            
            搜索
          </a>
        </li>
      
    </ul>
  

  
    <div class="site-search">
      
  <div class="popup search-popup local-search-popup">
  <div class="local-search-header clearfix">
    <span class="search-icon">
      <i class="fa fa-search"></i>
    </span>
    <span class="popup-btn-close">
      <i class="fa fa-times-circle"></i>
    </span>
    <div class="local-search-input-wrapper">
      <input autocomplete="off" placeholder="搜索..." spellcheck="false" type="text" id="local-search-input">
    </div>
  </div>
  <div id="local-search-result"></div>
</div>



    </div>
  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            
  <section id="posts" class="posts-expand">
    
      

  

  
  
  

  <article class="post post-type-normal" itemscope="" itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2018/11/23/PHP依赖注入/">

    <span hidden itemprop="author" itemscope="" itemtype="http://schema.org/Person">
      <meta itemprop="name" content="苏生不惑">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope="" itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="苏生不惑的博客">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/2018/11/23/PHP依赖注入/" itemprop="url">PHP依赖注入</a></h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2018-11-23T16:46:31+08:00">
                2018-11-23
              </time>
            

            

            
          </span>

          

          
            
          

          
          

          

          
            <div class="post-wordcount">
              
                
                  <span class="post-meta-divider">|</span>
                
                <span class="post-meta-item-icon">
                  <i class="fa fa-file-word-o"></i>
                </span>
                
                  <span class="post-meta-item-text">字数统计&#58;</span>
                
                <span title="字数统计">
                  3.8k 字
                </span>
              

              
                <span class="post-meta-divider">|</span>
              

              
                <span class="post-meta-item-icon">
                  <i class="fa fa-clock-o"></i>
                </span>
                
                  <span class="post-meta-item-text">阅读时长 &asymp;</span>
                
                <span title="阅读时长">
                  17 分钟
                </span>
              
            </div>
          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <p>本文转自<a href="https://laravel-china.org/articles/19554" target="_blank" rel="noopener">laravel-china</a></p>
<h3 id="依赖注入"><a href="#依赖注入" class="headerlink" title="依赖注入"></a>依赖注入</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br></pre></td><td class="code"><pre><span class="line">&lt;?php</span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Car</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">    <span class="comment">//汽车可以跑,定义一个方法 run()</span></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">run</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="string">'滴滴滴,车开了'</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Person</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">    private $car = <span class="literal">null</span>;<span class="comment">//保存某人的车,暂时还没车</span></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">drive</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">        $<span class="keyword">this</span>-&gt;car = <span class="keyword">new</span> Car();<span class="comment">//要开车先造车,造了一辆车保存在某人的 $car 里</span></span><br><span class="line">        <span class="keyword">return</span> $<span class="keyword">this</span>-&gt;car-&gt;run();<span class="comment">//调用车的 run() 方法</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">$xiaoming = <span class="keyword">new</span> Person();</span><br><span class="line">echo $xiaoming-&gt;drive();<span class="comment">//输出 滴滴滴,车开了</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Car</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">    <span class="comment">//汽车可以跑,定义一个方法 run()</span></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">run</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="string">'滴滴滴,车开了'</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Person</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">    private $car = <span class="literal">null</span>;<span class="comment">//保存某人的车,暂时还没车</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">//new 某人的时候,就给他注入一辆车,通过 $a 传入构造方法,并保存在 $car 里</span></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">__construct</span>(<span class="params">$a</span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $<span class="keyword">this</span>-&gt;car = $a;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">drive</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> $<span class="keyword">this</span>-&gt;car-&gt;run();<span class="comment">//调用车的 run() 方法</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">$car  = <span class="keyword">new</span> Car();<span class="comment">//买一辆车</span></span><br><span class="line">$xiaoming = <span class="keyword">new</span> Person($car);<span class="comment">//new 小明的时候,把刚才买的车注入</span></span><br><span class="line">echo $xiaoming-&gt;drive();<span class="comment">//输出 滴滴滴,车开了</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Car</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">    <span class="comment">//汽车可以跑,定义一个方法 run()</span></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">run</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="string">'滴滴滴,车开了'</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Person</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">    private $car = <span class="literal">null</span>;<span class="comment">//保存某人的车,暂时还没车</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">//new 某人的时候,就给他注入一辆车,通过 $a 传入构造方法</span></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">__construct</span>(<span class="params">Car $a</span>)// &lt;----这里做了类型限定</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $<span class="keyword">this</span>-&gt;car = $a;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">drive</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> $<span class="keyword">this</span>-&gt;car-&gt;run();<span class="comment">//调用车的 run() 方法</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="IoC-容器类"><a href="#IoC-容器类" class="headerlink" title="IoC 容器类"></a>IoC 容器类</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//写一个简单的 IoC 容器类</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Container</span></span>&#123;</span><br><span class="line">    private <span class="keyword">static</span> $objArr = [];<span class="comment">//定义一个静态的空数组</span></span><br><span class="line"></span><br><span class="line">    public <span class="keyword">static</span> <span class="function"><span class="keyword">function</span> <span class="title">set</span>(<span class="params">$flag, Callable $func</span>)</span>&#123;</span><br><span class="line">        self::$objArr[$flag] = $func;<span class="comment">//存入键值对,键是一个字符串,作为标识符,值是一个匿名函数</span></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public <span class="keyword">static</span> <span class="function"><span class="keyword">function</span> <span class="title">get</span>(<span class="params">$flag</span>)</span>&#123;</span><br><span class="line">        $tmp = self::$objArr[$flag];<span class="comment">//取出标识符对应的匿名函数,用$tmp临时保存一下</span></span><br><span class="line">        <span class="keyword">return</span> $tmp();<span class="comment">//在$tmp后名加上括号,表示执行这个函数,并返回</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//下面这条语句执行完毕后,会在 $objArr 里存入一个键值对,键是 car ,值是这个匿名函数,该匿名函数返回的是创建 Car 对象的语句</span></span><br><span class="line">Container::set(<span class="string">'Car'</span>, <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="keyword">new</span> Car();</span><br><span class="line">&#125;);</span><br><span class="line"></span><br><span class="line"><span class="comment">//下面这条语句执行完毕后,会在 $objArr 里存入一个键值对,键是 person ,值是这个匿名函数,该匿名函数返回的是创建 Person 对象的语句</span></span><br><span class="line">Container::set(<span class="string">'Person'</span>, <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="keyword">new</span> Person(Container::get(<span class="string">'Car'</span>));<span class="comment">//直接去容器中取一辆车出来,并作为参数传给 Person 类的构造函数</span></span><br><span class="line">&#125;);</span><br><span class="line"></span><br><span class="line">$xiaomin = Container::get(<span class="string">'Person'</span>);<span class="comment">//直接去容器中取一个人出来,取名叫小明</span></span><br><span class="line">echo $xiaomin-&gt;drive();<span class="comment">//输出 滴滴滴,车开了</span></span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Container</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">    protected $binds;</span><br><span class="line"></span><br><span class="line">    protected $instances;</span><br><span class="line"></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">bind</span>(<span class="params">$abstract, $concrete</span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="keyword">if</span> ($concrete <span class="keyword">instanceof</span> Closure) &#123;</span><br><span class="line">            $<span class="keyword">this</span>-&gt;binds[$abstract] = $concrete;</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            $<span class="keyword">this</span>-&gt;instances[$abstract] = $concrete;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">make</span>(<span class="params">$abstract, $parameters = []</span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="keyword">if</span> (isset($<span class="keyword">this</span>-&gt;instances[$abstract])) &#123;</span><br><span class="line">            <span class="keyword">return</span> $<span class="keyword">this</span>-&gt;instances[$abstract];</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        array_unshift($parameters, $<span class="keyword">this</span>);</span><br><span class="line"></span><br><span class="line">        <span class="keyword">return</span> call_user_func_array($<span class="keyword">this</span>-&gt;binds[$abstract], $parameters);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//在线执行laravel https://implode.io/4mT8O4</span></span><br><span class="line"> $container = <span class="keyword">new</span> Container;</span><br><span class="line"></span><br><span class="line">    $container-&gt;bind(<span class="string">'Board'</span>, <span class="function"><span class="keyword">function</span>(<span class="params">$container</span>)</span>&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">new</span> CommonBoard;</span><br><span class="line">    &#125;);</span><br><span class="line"></span><br><span class="line">    $container-&gt;bind(<span class="string">'Computer'</span>,<span class="function"><span class="keyword">function</span>(<span class="params">$container,$module</span>)</span>&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">new</span> Computer($container-&gt;make($<span class="built_in">module</span>));</span><br><span class="line">    &#125;);</span><br><span class="line"></span><br><span class="line">    $computer = $container-&gt;make(<span class="string">'Computer'</span>,[<span class="string">'Board'</span>]);</span><br></pre></td></tr></table></figure>
<h3 id="魔术方法"><a href="#魔术方法" class="headerlink" title="魔术方法"></a>魔术方法</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//https://laravel-china.org/articles/20625</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Model</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">    protected <span class="function"><span class="keyword">function</span> <span class="title">increment</span>(<span class="params">$column, $amount = <span class="number">1</span>, array $extra = []</span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> $<span class="keyword">this</span>-&gt;incrementOrDecrement($column, $amount, $extra, <span class="string">'increment'</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">__call</span>(<span class="params">$method, $parameters</span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="keyword">if</span> (in_array($method, [<span class="string">'increment'</span>, <span class="string">'decrement'</span>])) &#123;</span><br><span class="line">            <span class="keyword">return</span> $<span class="keyword">this</span>-&gt;$method(...$parameters);</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">return</span> $<span class="keyword">this</span>-&gt;forwardCallTo($<span class="keyword">this</span>-&gt;newQuery(), $method, $parameters);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public <span class="keyword">static</span> <span class="function"><span class="keyword">function</span> <span class="title">__callStatic</span>(<span class="params">$method, $parameters</span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> (<span class="keyword">new</span> <span class="keyword">static</span>)-&gt;$method(...$parameters);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">(<span class="keyword">new</span> User)-&gt;increment(<span class="string">'age'</span>);</span><br><span class="line">User::increment(<span class="string">'age'</span>);</span><br><span class="line">第一种，因为我们User继承了Model类，但是increment方法前有个protected导致我们无法从外部访问这个方法。但是不用慌张，这个时候__call魔术方法就起到了效果，他会帮我们去访问increment方法。</span><br><span class="line"></span><br><span class="line">第二种，我们用静态调用increment方法，运行的时候，程序就去找有没有定义的静态increment方法，找了一圈没有找到，怎么办？这个时候__callStatic开始发挥作用。我们用的是User类，因为延迟静态绑定的缘故，可以看成:</span><br><span class="line"></span><br><span class="line"><span class="keyword">return</span> (<span class="keyword">new</span> User)-&gt;increment(<span class="string">'age'</span>);</span><br></pre></td></tr></table></figure>
<h3 id="控制反转"><a href="#控制反转" class="headerlink" title="控制反转"></a>控制反转</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Boos</span></span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="comment">//领导依赖员工</span></span><br><span class="line">    private $staff;</span><br><span class="line"></span><br><span class="line">    <span class="comment">//现在老板只需要接受 hr 招聘就好，将控制权交给 hr</span></span><br><span class="line">    <span class="comment">//以设置方法来实现依赖注入</span></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">setStaff</span>(<span class="params">Standard $staff</span>)</span>&#123;      </span><br><span class="line">        $<span class="keyword">this</span>-&gt;staff = $staff;</span><br><span class="line">    &#125; </span><br><span class="line"></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">task</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">        $<span class="keyword">this</span>-&gt;staff-&gt;work();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//招聘所设定的标准</span></span><br><span class="line">interface Standard&#123;</span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">work</span>(<span class="params"></span>);</span></span><br><span class="line"><span class="function">&#125;</span></span><br><span class="line"><span class="function"></span></span><br><span class="line"><span class="function">//员工需要依赖的标准</span></span><br><span class="line"><span class="function"><span class="title">class</span> <span class="title">StaffA</span> <span class="title">implements</span> <span class="title">Standard</span></span>&#123;</span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">work</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">        echo <span class="string">'雇员A有能力能够完成老板指定的工作'</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">StaffB</span> <span class="title">implements</span> <span class="title">Standard</span></span>&#123;</span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">work</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">        echo <span class="string">'雇员B有能力能够完成老板指定的工作'</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//ioc容器</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Hr</span></span>&#123;</span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">getStagff</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">new</span> StaffB();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//公司老板</span></span><br><span class="line">$boos = <span class="keyword">new</span> Boos();</span><br><span class="line"><span class="comment">//老板招的hr</span></span><br><span class="line">$hr = <span class="keyword">new</span> Hr();</span><br><span class="line">$staff = $hr-&gt;getStagff();</span><br><span class="line"><span class="comment">//hr把招到的人给老板（控制反转和依赖注入）</span></span><br><span class="line">$boos-&gt;setStaff($staff);</span><br><span class="line"><span class="comment">//老板让他工作了</span></span><br><span class="line">$boos-&gt;task();</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Boos</span></span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="comment">//领导依赖员工</span></span><br><span class="line">    private $staff;</span><br><span class="line"></span><br><span class="line">    <span class="comment">//老板只需要告诉外部我需要什么样的人就好了，其它什么都不管，具体什么样的人交给外部处理。</span></span><br><span class="line">    <span class="comment">//用构造方法方式实现依赖注入</span></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">__construct</span>(<span class="params">Standard $staff</span>)</span>&#123;       </span><br><span class="line">        $<span class="keyword">this</span>-&gt;staff = $staff;</span><br><span class="line">    &#125; </span><br><span class="line"></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">task</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">        $<span class="keyword">this</span>-&gt;staff-&gt;work();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//招聘所设定的标准</span></span><br><span class="line">interface Standard&#123;</span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">work</span>(<span class="params"></span>);</span></span><br><span class="line"><span class="function">&#125;</span></span><br><span class="line"><span class="function"></span></span><br><span class="line"><span class="function">//员工需要依赖的标准</span></span><br><span class="line"><span class="function"><span class="title">class</span> <span class="title">StaffA</span> <span class="title">implements</span> <span class="title">Standard</span></span>&#123;</span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">work</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">        echo <span class="string">'雇员A有能力能够完成老板指定的工作'</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">StaffB</span> <span class="title">implements</span> <span class="title">Standard</span></span>&#123;</span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">work</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">        echo <span class="string">'雇员B有能力能够完成老板指定的工作'</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Hr</span></span>&#123;</span><br><span class="line"></span><br><span class="line">    private $binds = [];</span><br><span class="line"></span><br><span class="line">    <span class="comment">//接受不同员工的简历，并存起来</span></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">bind</span>(<span class="params">$contract,$concrete</span>)</span>&#123;</span><br><span class="line">        $<span class="keyword">this</span>-&gt;binds[$contract] = $concrete;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">//询问老板选人的标准由哪些,并且从满足的简历中筛选人</span></span><br><span class="line">    private <span class="function"><span class="keyword">function</span> <span class="title">methodBindParams</span>(<span class="params">$className</span>)</span>&#123;</span><br><span class="line">        $reflect = <span class="keyword">new</span> reflect($className,<span class="string">'__construct'</span>);</span><br><span class="line">        <span class="keyword">return</span> $reflect-&gt;bindParamsToMethod();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">//将选好的工作人员交给老板</span></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">make</span>(<span class="params">$className</span>)</span>&#123;</span><br><span class="line">        $methodBindParams = $<span class="keyword">this</span>-&gt;methodBindParams($className);</span><br><span class="line">        $reflect = <span class="keyword">new</span> reflect($className,<span class="string">'__construct'</span>);</span><br><span class="line">        <span class="keyword">return</span> $reflect-&gt;make($<span class="keyword">this</span>-&gt;binds,$methodBindParams);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">reflect</span></span>&#123;</span><br><span class="line">    private $className;</span><br><span class="line"></span><br><span class="line">    private $methodName;</span><br><span class="line"></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">__construct</span>(<span class="params">$className,$methodName</span>)</span>&#123;</span><br><span class="line">        $<span class="keyword">this</span>-&gt;className = $className;</span><br><span class="line">        $<span class="keyword">this</span>-&gt;methodName = $methodName;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">//绑定参数到方法</span></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">bindParamsToMethod</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line"></span><br><span class="line">        $params = [];</span><br><span class="line"></span><br><span class="line">        $method  = <span class="keyword">new</span> ReflectionMethod($<span class="keyword">this</span>-&gt;className,$<span class="keyword">this</span>-&gt;methodName);</span><br><span class="line"></span><br><span class="line">        foreach ($method-&gt;getParameters() <span class="keyword">as</span> $param) &#123;</span><br><span class="line">            $params[] =  [$param-&gt;name,$param-&gt;getClass()-&gt;name];</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">return</span> [$<span class="keyword">this</span>-&gt;<span class="function"><span class="params">className</span>=&gt;</span> $params];</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">make</span>(<span class="params">$bind,$methodBindParams</span>)</span>&#123;</span><br><span class="line">        $args = [];</span><br><span class="line">        foreach ($methodBindParams <span class="keyword">as</span> $className =&gt; $params) &#123;</span><br><span class="line">            foreach ($params <span class="keyword">as</span> $param) &#123;</span><br><span class="line">                list($paramName,$paramType) = $param;</span><br><span class="line"></span><br><span class="line">                $paramName = <span class="keyword">new</span> $bind[$paramType]();</span><br><span class="line"></span><br><span class="line">                array_push($args, $paramName);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        $reflectionClass = <span class="keyword">new</span> ReflectionClass($<span class="keyword">this</span>-&gt;className);</span><br><span class="line">        <span class="keyword">return</span> $reflectionClass-&gt;newInstanceArgs($args);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line">$hr = <span class="keyword">new</span> Hr();</span><br><span class="line"></span><br><span class="line"><span class="comment">//老板如果需要换工作人员，只需要绑定其它的工作人员即可。</span></span><br><span class="line">$staff = $hr-&gt;bind(<span class="string">'Standard'</span>,<span class="string">'StaffA'</span>);</span><br><span class="line"></span><br><span class="line">$boos = $hr-&gt;make(<span class="string">'Boos'</span>);</span><br><span class="line"></span><br><span class="line">$boos-&gt;task();</span><br></pre></td></tr></table></figure>
<h3 id="依赖注入原理"><a href="#依赖注入原理" class="headerlink" title="依赖注入原理"></a>依赖注入原理</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br></pre></td><td class="code"><pre><span class="line">&lt;?php</span><br><span class="line"></span><br><span class="line">namespace Database;</span><br><span class="line">use ReflectionMethod;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Database</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line"></span><br><span class="line">    protected $adapter;</span><br><span class="line"></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">__construct</span> (<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;&#125;</span><br><span class="line"></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">test</span> (<span class="params">MysqlAdapter $adapter</span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $adapter-&gt;test();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">MysqlAdapter</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line"></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">test</span> (<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        echo <span class="string">"i am MysqlAdapter test"</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">app</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line"></span><br><span class="line">    public <span class="keyword">static</span> <span class="function"><span class="keyword">function</span> <span class="title">run</span> (<span class="params">$instance, $method</span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="keyword">if</span> (! method_exists($instance, $method))</span><br><span class="line"></span><br><span class="line">            <span class="keyword">return</span> <span class="literal">null</span>;</span><br><span class="line"></span><br><span class="line">        $reflector = <span class="keyword">new</span> ReflectionMethod($instance, $method);</span><br><span class="line"></span><br><span class="line">        $parameters = [</span><br><span class="line">                    <span class="number">1</span></span><br><span class="line">            ];</span><br><span class="line">        foreach ($reflector-&gt;getParameters() <span class="keyword">as</span> $key =&gt; $parameter)</span><br><span class="line">        &#123;</span><br><span class="line"></span><br><span class="line">            $<span class="class"><span class="keyword">class</span> </span>= $parameter-&gt;getClass();</span><br><span class="line"></span><br><span class="line">            <span class="keyword">if</span> ($<span class="class"><span class="keyword">class</span>)</span></span><br><span class="line"><span class="class">            </span>&#123;</span><br><span class="line">                array_splice($parameters, $key, <span class="number">0</span>, [</span><br><span class="line">                        <span class="keyword">new</span> $<span class="class"><span class="keyword">class</span>-&gt;<span class="title">name</span>()</span></span><br><span class="line">                ]);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        call_user_func_array([</span><br><span class="line">                $instance,</span><br><span class="line">                $method</span><br><span class="line">        ], $parameters);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">app::run(<span class="keyword">new</span> Database(), <span class="string">'test'</span>);</span><br></pre></td></tr></table></figure>
<h3 id="反射机制实现依赖注入"><a href="#反射机制实现依赖注入" class="headerlink" title="反射机制实现依赖注入"></a>反射机制实现依赖注入</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br></pre></td><td class="code"><pre><span class="line">&lt;?php</span><br><span class="line">namespace Models;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Car</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">namespace Framework;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">App</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">getInstance</span>(<span class="params">$className</span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="comment">//实例化 ReflectionClass 对象</span></span><br><span class="line">        $reflector = <span class="keyword">new</span> \ReflectionClass($className);</span><br><span class="line"></span><br><span class="line">        <span class="keyword">if</span> (!$reflector-&gt;isInstantiable()) &#123;</span><br><span class="line">            <span class="comment">//不能被实例化的逻辑</span></span><br><span class="line">            <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="comment">//获取构造器</span></span><br><span class="line">        $<span class="keyword">constructor</span> = $reflector-&gt;getConstructor();</span><br><span class="line"></span><br><span class="line">        //如果没有构造器，直接实例化</span><br><span class="line">        if (!$<span class="keyword">constructor</span>) &#123;</span><br><span class="line">            <span class="comment">//这里用了变量来动态的实例化类</span></span><br><span class="line">            <span class="keyword">return</span> <span class="keyword">new</span> $className;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">$app = <span class="keyword">new</span> App();</span><br><span class="line">$car = $app-&gt;getInstance(<span class="string">'Models\Car'</span>);</span><br><span class="line">var_dump($car); <span class="comment">//输出 object(Models\Car)#4 (0) &#123; &#125;</span></span><br><span class="line">&lt;?php</span><br><span class="line">namespace Framework;</span><br><span class="line"></span><br><span class="line"><span class="comment">//定义一个类，用于实现依赖注入</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">App</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">getInstance</span>(<span class="params">$className</span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="comment">//实例化 ReflectionClass 对象</span></span><br><span class="line">        $reflector = <span class="keyword">new</span> \ReflectionClass($className);</span><br><span class="line"></span><br><span class="line">        <span class="keyword">if</span> (!$reflector-&gt;isInstantiable()) &#123;</span><br><span class="line">            <span class="comment">//不能被实例化的逻辑，抽象类和接口不能被实例化</span></span><br><span class="line">            <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="comment">//获取构造器</span></span><br><span class="line">        $<span class="keyword">constructor</span> = $reflector-&gt;getConstructor();</span><br><span class="line"></span><br><span class="line">        //如果没有构造器，也就是没有依赖，直接实例化</span><br><span class="line">        if (!$<span class="keyword">constructor</span>) &#123;</span><br><span class="line">            <span class="keyword">return</span> <span class="keyword">new</span> $className;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="comment">//如果有构造器，先把构造器中的参数获取出来</span></span><br><span class="line">        $parameters = $<span class="keyword">constructor</span>-&gt;getParameters();</span><br><span class="line"></span><br><span class="line">        //再遍历 parameters ，找出每一个类的依赖，存到 dependencies 数组中</span><br><span class="line">        $dependencies = array_map(function ($parameter) &#123;</span><br><span class="line">            <span class="comment">/**</span></span><br><span class="line"><span class="comment">             * 这里是递归的去寻找每一个类的依赖，例如第一次执行的时候，程序发现汽车 Car 类依赖底盘 Chassis</span></span><br><span class="line"><span class="comment">             * 类，此时 $parameter 是一个ReflectionParameter 的实例，接着调用 ReflectionParameter</span></span><br><span class="line"><span class="comment">             * 的 getClass() 方法，获得一个 ReflectionClass 的实例，再接着调用 ReflectionClass</span></span><br><span class="line"><span class="comment">             * 的 getName() 方法，取得类名，也就是 Models\Chassis ，但此时此刻还不能直接去 new</span></span><br><span class="line"><span class="comment">             * Models\Chassis ，因为 Models\Chassis 也有依赖，故要递归的去调用 getInstance</span></span><br><span class="line"><span class="comment">             * 进一步去寻找该类的依赖，周而复始，直到触发上面的 if(!$constructor) ，停止递归。</span></span><br><span class="line"><span class="comment">             */</span></span><br><span class="line">            <span class="keyword">return</span> $<span class="keyword">this</span>-&gt;getInstance($parameter-&gt;getClass()-&gt;getName());</span><br><span class="line">        &#125;, $parameters);</span><br><span class="line"></span><br><span class="line">        <span class="comment">//最后，使用 ReflectionClass 类提供的 newInstanceArgs ，方法去实例化类，参数将会传入构造器中</span></span><br><span class="line">        <span class="keyword">return</span> $reflector-&gt;newInstanceArgs($dependencies);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">namespace Models;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Car</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">    protected $chassis;</span><br><span class="line"></span><br><span class="line">    <span class="comment">//汽车依赖底盘</span></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">__construct</span>(<span class="params">Chassis $chassis</span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $<span class="keyword">this</span>-&gt;chassis = $chassis;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Chassis</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">    protected $tyre;</span><br><span class="line">    protected $axle;</span><br><span class="line"></span><br><span class="line">    <span class="comment">//底盘依赖轮胎和轴承</span></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">__construct</span>(<span class="params">Tyre $tyre, Axle $axle</span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $<span class="keyword">this</span>-&gt;tyre = $tyre;</span><br><span class="line">        $<span class="keyword">this</span>-&gt;axle = $axle;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Tyre</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">    protected $axle;</span><br><span class="line"></span><br><span class="line">    <span class="comment">//轮胎也依赖轴承</span></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">__construct</span>(<span class="params">Axle $axle</span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $<span class="keyword">this</span>-&gt;axle = $axle;</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Axle</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">    <span class="comment">//轴承无依赖</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">$app = <span class="keyword">new</span> \Framework\App();</span><br><span class="line">$car = $app-&gt;getInstance(<span class="string">'Models\Car'</span>);<span class="comment">//不用$car = new Car(new Chassis(new Tyre(new Axle), new Axle()))</span></span><br><span class="line">var_dump($car);</span><br></pre></td></tr></table></figure>
<h3 id="IOC"><a href="#IOC" class="headerlink" title="IOC"></a>IOC</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br></pre></td><td class="code"><pre><span class="line">&lt;?php</span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 没有IoC/DI的时候，常规的A类使用C类的示例</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * Class c</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">c</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">say</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        echo <span class="string">'hello'</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * Class a</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">a</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">    private $c;</span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">__construct</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $<span class="keyword">this</span>-&gt;c = <span class="keyword">new</span> C(); <span class="comment">// 实例化创建C类</span></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">sayC</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        echo $<span class="keyword">this</span>-&gt;c-&gt;say(); <span class="comment">// 调用C类中的方法</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">$a = <span class="keyword">new</span> a();</span><br><span class="line">$a-&gt;sayC();</span><br><span class="line">&lt;?php</span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 当有了IoC/DI的容器后,a类依赖c实例注入的示例https://segmentfault.com/a/1190000007536704</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * Class c</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">c</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">say</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        echo <span class="string">'hello'</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * Class a</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">a</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">    private $c;</span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">setC</span>(<span class="params">C $c</span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $<span class="keyword">this</span>-&gt;c = $c; <span class="comment">// 实例化创建C类</span></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">sayC</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        echo $<span class="keyword">this</span>-&gt;c-&gt;say(); <span class="comment">// 调用C类中的方法</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">$c = <span class="keyword">new</span> C();</span><br><span class="line">$a = <span class="keyword">new</span> a();</span><br><span class="line">$a-&gt;setC($c);</span><br><span class="line">$a-&gt;sayC();</span><br></pre></td></tr></table></figure>
<h3 id="依赖注入-控制反转"><a href="#依赖注入-控制反转" class="headerlink" title="依赖注入 控制反转"></a>依赖注入 控制反转</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br></pre></td><td class="code"><pre><span class="line">interface DbConnect &#123;</span><br><span class="line"></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">connect</span>(<span class="params"></span>);</span></span><br><span class="line"><span class="function">&#125;</span></span><br><span class="line"><span class="function"></span></span><br><span class="line"><span class="function"><span class="title">class</span> <span class="title">DbA</span> <span class="title">implements</span> <span class="title">DbConnect</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">connect</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="comment">// <span class="doctag">TODO:</span> Implement test_echo() method.</span></span><br><span class="line">        echo <span class="string">'A'</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">DbB</span> <span class="title">implements</span> <span class="title">DbConnect</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">connect</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="comment">// <span class="doctag">TODO:</span> Implement test_echo() method.</span></span><br><span class="line">        echo <span class="string">"B"</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">ConnectDb</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    protected $connects;</span><br><span class="line"></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">__construct</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $<span class="keyword">this</span>-&gt;connect = <span class="keyword">new</span> DbA();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">connect</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        echo $<span class="keyword">this</span>-&gt;connect-&gt;connect();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">上面写法可以实现数据库连接功能，但是要切换到 B 数据库就要修改 ConnectDb 类，这样代码没法达到低耦合，也不符合编程开放封闭原则。我们可以把连接的类用函数传参的方式传进 ConnectDb 构造函数里.</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">ConnectDb</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    protected $connects;</span><br><span class="line"></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">__construct</span>(<span class="params">DbConnect $dbConnect</span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $<span class="keyword">this</span>-&gt;connect = $dbConnect;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">connect</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        echo $<span class="keyword">this</span>-&gt;connect-&gt;connect();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">$ConnectDb = <span class="keyword">new</span> ConnectDb(<span class="keyword">new</span> DbA());</span><br><span class="line">$ConnectDb-&gt;connect();</span><br><span class="line">这样我们就可以不修改 ConnectDb 而是用构造函数参数传递的方式切换数据库，这就是控制反转（感觉好像工厂模式。。。），不需要自己内容修改，通过外部传递参数，这种由外部负责其依赖需求的行为，称控制反转（IoC）</span><br><span class="line"></span><br><span class="line">不通过自己内部 <span class="keyword">new</span> 对象或者实例，而是通过函数或者构造函数传递进来。称为依赖注入（DI）</span><br><span class="line"></span><br><span class="line">反射</span><br><span class="line"></span><br><span class="line">刚毕业那会面试一家公司面试就问我了不了解反射，当时给我问的一懵。工作这么长时间了其实还是不了解。。。。</span><br><span class="line"></span><br><span class="line">反射其实就是根据类名返回这个类的任何信息，比如该类的方法，参数，属性等等</span><br><span class="line"></span><br><span class="line">ReflectionClass 类报告了一个类的有关信息。</span><br><span class="line"><span class="comment">//   — 初始化 ReflectionClass 类</span></span><br><span class="line">$reflectionClass = <span class="keyword">new</span> ReflectionClass(<span class="string">'ConnectDb'</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">//   — 获取类的构造函数   一个 ReflectionMethod 对象，反射了类的构造函数，或者当类不存在构造函数时返回 NULL。</span></span><br><span class="line">$<span class="keyword">constructor</span>     = $reflectionClass-&gt;getConstructor();</span><br><span class="line"></span><br><span class="line">//   - 获取构造函数所有依赖参数</span><br><span class="line">$dependencies    = $<span class="keyword">constructor</span>-&gt;getParameters();</span><br><span class="line"></span><br><span class="line">//   - 创建类的新的实例。给出的参数将会传递到类的构造函数。</span><br><span class="line">$ConnectDb       = $reflectionClass-&gt;newInstance();</span><br><span class="line"></span><br><span class="line">//   - 创建一个类的新实例，给出的参数将传递到类的构造函数。这个参数以 array 形式传递到类的构造函数 返回值 返回类的实例</span><br><span class="line">$ConnectDb       = $reflectionClass-&gt;newInstanceArgs($dependencies);</span><br><span class="line">我们可以创建一个方法，利用反射的机制拿到 ConnectDb 类的构造函数，然后拿到构造函数的参数对象，用递归的方法创建参数依赖，最后调用 newInstanceArgs 方法生成 ConnectDb 实例</span><br><span class="line"></span><br><span class="line">interface DbConnect &#123;</span><br><span class="line"></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">connect</span>(<span class="params"></span>);</span></span><br><span class="line"><span class="function">&#125;</span></span><br><span class="line"><span class="function"></span></span><br><span class="line"><span class="function"><span class="title">class</span> <span class="title">DbA</span> <span class="title">implements</span> <span class="title">DbConnect</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">connect</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="comment">// <span class="doctag">TODO:</span> Implement test_echo() method.</span></span><br><span class="line">        echo <span class="string">'A'</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">DbB</span> <span class="title">implements</span> <span class="title">DbConnect</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">connect</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="comment">// <span class="doctag">TODO:</span> Implement test_echo() method.</span></span><br><span class="line">        echo <span class="string">"B"</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">ConnectDb</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    protected $connects;</span><br><span class="line">    <span class="comment">//反射是不能动态创建接口的</span></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">__construct</span>(<span class="params">DbA $dbConnect</span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $<span class="keyword">this</span>-&gt;connect = $dbConnect;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">connect</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        echo $<span class="keyword">this</span>-&gt;connect-&gt;connect();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">make</span> (<span class="params">$concrete</span>) </span>&#123;</span><br><span class="line">    <span class="comment">//   — 初始化 ReflectionClass 类</span></span><br><span class="line">    $reflectionClass = <span class="keyword">new</span> ReflectionClass($concrete);</span><br><span class="line">    <span class="comment">//   — 获取类的构造函数   一个 ReflectionMethod 对象，反射了类的构造函数，或者当类不存在构造函数时返回 NULL。</span></span><br><span class="line">    $<span class="keyword">constructor</span>     = $reflectionClass-&gt;getConstructor();</span><br><span class="line"></span><br><span class="line">    //如果不需要传递参数直接返回实例</span><br><span class="line">    if (is_null($<span class="keyword">constructor</span>)) &#123;</span><br><span class="line">        <span class="keyword">return</span> $reflectionClass-&gt;newInstance();</span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">    <span class="comment">//   - 获取构造函数所有依赖参数</span></span><br><span class="line">        $dependencies    = $<span class="keyword">constructor</span>-&gt;getParameters();</span><br><span class="line">        $instance = getdependencies($dependencies);</span><br><span class="line">        return $reflectionClass-&gt;newInstanceArgs($instance);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">function getdependencies($parameter)</span><br><span class="line">&#123;</span><br><span class="line">    $dependencies = [];</span><br><span class="line">    foreach ($parameter <span class="keyword">as</span> $paramete) &#123;</span><br><span class="line">        <span class="comment">//返回对象实例 object 所属类的名字。返回对象实例 object 所属类的名字。 如果 object 不是一个对象则返回 FALSE。</span></span><br><span class="line">        $dependencies[] = make($paramete-&gt;getClass()-&gt;name);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> $dependencies;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">$ConnectDb = make(<span class="string">'ConnectDb'</span>);</span><br><span class="line">$ConnectDb-&gt;connect();</span><br><span class="line">总结：</span><br><span class="line"></span><br><span class="line">控制反转（IoC）：不修改类内部自身，通过外部参数传递的方式，由外部负责其依赖需求的行为。</span><br><span class="line">依赖注入（DI） ：不通过自己 <span class="keyword">new</span> 或实例实例化，通过函数或者构造函数传递进来。</span><br><span class="line">反射：根据类名获取类的信息，方法、属性、参数等等https:<span class="comment">//learnku.com/articles/32310</span></span><br></pre></td></tr></table></figure>
<h3 id="IOC-demo"><a href="#IOC-demo" class="headerlink" title="IOC demo"></a>IOC demo</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">interface Board &#123;</span><br><span class="line">        public <span class="function"><span class="keyword">function</span> <span class="title">type</span>(<span class="params"></span>);</span></span><br><span class="line"><span class="function">    &#125;</span></span><br><span class="line"><span class="function"></span></span><br><span class="line"><span class="function">    <span class="title">class</span> <span class="title">CommonBoard</span> <span class="title">implements</span> <span class="title">Board</span> </span>&#123;</span><br><span class="line">        public <span class="function"><span class="keyword">function</span> <span class="title">type</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">            echo <span class="string">'普通键盘'</span>;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="class"><span class="keyword">class</span> <span class="title">MechanicalKeyboard</span> <span class="title">implements</span> <span class="title">Board</span> </span>&#123;</span><br><span class="line">        public <span class="function"><span class="keyword">function</span> <span class="title">type</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">            echo <span class="string">'机械键盘'</span>;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="class"><span class="keyword">class</span> <span class="title">Computer</span> </span>&#123;</span><br><span class="line">        protected $keyboard;</span><br><span class="line"></span><br><span class="line">        public <span class="function"><span class="keyword">function</span> <span class="title">__construct</span> (<span class="params">Board $keyboard</span>) </span>&#123;</span><br><span class="line">            $<span class="keyword">this</span>-&gt;keyboard = $keyboard;</span><br><span class="line">        &#125;</span><br><span class="line">        public <span class="function"><span class="keyword">function</span> <span class="title">type</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">            $<span class="keyword">this</span>-&gt;keyboard-&gt;type();</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    $computer = <span class="keyword">new</span> Computer(<span class="keyword">new</span> MechanicalKeyboard());</span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Container</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">    protected $binds;</span><br><span class="line"></span><br><span class="line">    protected $instances;</span><br><span class="line"></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">bind</span>(<span class="params">$abstract, $concrete</span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="keyword">if</span> ($concrete <span class="keyword">instanceof</span> Closure) &#123;</span><br><span class="line">            $<span class="keyword">this</span>-&gt;binds[$abstract] = $concrete;</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            $<span class="keyword">this</span>-&gt;instances[$abstract] = $concrete;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">make</span>(<span class="params">$abstract, $parameters = []</span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="keyword">if</span> (isset($<span class="keyword">this</span>-&gt;instances[$abstract])) &#123;</span><br><span class="line">            <span class="keyword">return</span> $<span class="keyword">this</span>-&gt;instances[$abstract];</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        array_unshift($parameters, $<span class="keyword">this</span>);<span class="comment">//闭包的时候第一个参数为$this</span></span><br><span class="line"></span><br><span class="line">        <span class="keyword">return</span> call_user_func_array($<span class="keyword">this</span>-&gt;binds[$abstract], $parameters);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">   $container = <span class="keyword">new</span> Container;</span><br><span class="line"></span><br><span class="line">    $container-&gt;bind(<span class="string">'Board'</span>, <span class="function"><span class="keyword">function</span>(<span class="params">$container</span>)</span>&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">new</span> CommonBoard;</span><br><span class="line">    &#125;);</span><br><span class="line">    <span class="comment">//当我需要Computer类的时候你就给我实例化Computer类</span></span><br><span class="line">    $container-&gt;bind(<span class="string">'Computer'</span>,<span class="function"><span class="keyword">function</span>(<span class="params">$container,$module</span>)</span>&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">new</span> Computer($container-&gt;make($<span class="built_in">module</span>));</span><br><span class="line">    &#125;);</span><br><span class="line"></span><br><span class="line">    $computer = $container-&gt;make(<span class="string">'Computer'</span>,[<span class="string">'Board'</span>]);<span class="comment">//对Computer进行生产返回一个实例。</span></span><br><span class="line">    echo $computer-&gt;type();</span><br><span class="line"></span><br><span class="line">    <span class="comment">//更换键盘怎么办呢？</span></span><br><span class="line"></span><br><span class="line">    $container-&gt;bind(<span class="string">'Board'</span>, <span class="function"><span class="keyword">function</span>(<span class="params">$container</span>)</span>&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">new</span> MechanicalKeyboard;</span><br><span class="line">    &#125;);</span><br><span class="line"></span><br><span class="line">    $container-&gt;bind(<span class="string">'Computer'</span>,<span class="function"><span class="keyword">function</span>(<span class="params">$container,$module</span>)</span>&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">new</span> Computer($container-&gt;make($<span class="built_in">module</span>));</span><br><span class="line">    &#125;);</span><br><span class="line"></span><br><span class="line">    $computer = $container-&gt;make(<span class="string">'Computer'</span>,[<span class="string">'Board'</span>]);</span><br><span class="line">    <span class="comment">//https://3v4l.org/3krab</span></span><br></pre></td></tr></table></figure>
<p><a href="https://learnku.com/articles/24200" target="_blank" rel="noopener">反射机制实现依赖注入</a></p>
<p><a href="https://phpcasts.org/topics/21" target="_blank" rel="noopener">PHP中的依赖注入（DI）容器</a></p>
<p><a href="https://laravel-china.org/articles/19195" target="_blank" rel="noopener">Laravel 服务容器，IoC,DI</a></p>
<p><a href="https://laravel-china.org/topics/843/laravel-dependency-injection-principle" target="_blank" rel="noopener"> Laravel 依赖注入原理</a></p>
<p><a href="https://laravel-china.org/articles/14145/rely-on-inversion-control-inversion-ioc-container-dependency-injection" target="_blank" rel="noopener">浅析依赖倒转、控制反转、IoC 容器、依赖注入</a></p>

          
        
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope="" itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2018/11/20/hello-world/">

    <span hidden itemprop="author" itemscope="" itemtype="http://schema.org/Person">
      <meta itemprop="name" content="苏生不惑">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope="" itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="苏生不惑的博客">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/2018/11/20/hello-world/" itemprop="url">hexo搭建博客</a></h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2018-11-20T10:08:23+08:00">
                2018-11-20
              </time>
            

            

            
          </span>

          

          
            
          

          
          

          

          
            <div class="post-wordcount">
              
                
                  <span class="post-meta-divider">|</span>
                
                <span class="post-meta-item-icon">
                  <i class="fa fa-file-word-o"></i>
                </span>
                
                  <span class="post-meta-item-text">字数统计&#58;</span>
                
                <span title="字数统计">
                  6k 字
                </span>
              

              
                <span class="post-meta-divider">|</span>
              

              
                <span class="post-meta-item-icon">
                  <i class="fa fa-clock-o"></i>
                </span>
                
                  <span class="post-meta-item-text">阅读时长 &asymp;</span>
                
                <span title="阅读时长">
                  32 分钟
                </span>
              
            </div>
          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <p>通常我们可以使用github pages 来搭建静态博客，建立一个username.github.io的项目就可以了，如果要将其他项目也作为页面展示，可以将代码推送到gh-pages分支。</p>
<p>GitHub pages木有默认样式，所以如果你不会自己写css，博客很难看的，所以有了hexo.</p>
<h3 id="准备"><a href="#准备" class="headerlink" title="准备"></a>准备</h3><p>先安装好git node hexo</p>
<h4 id="初始化"><a href="#初始化" class="headerlink" title="初始化"></a>初始化</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br></pre></td><td class="code"><pre><span class="line">$ hexo init blog</span><br><span class="line">INFO  Cloning hexo-starter to D:\code\hexo\blog</span><br><span class="line">Cloning into <span class="string">'D:\code\hexo\blog'</span>...</span><br><span class="line">remote: Enumerating objects: <span class="number">68</span>, done.</span><br><span class="line">remote: Total <span class="number">68</span> (delta <span class="number">0</span>), reused <span class="number">0</span> (delta <span class="number">0</span>), pack-reused <span class="number">68</span></span><br><span class="line">Unpacking objects: <span class="number">100</span>% (<span class="number">68</span>/<span class="number">68</span>), done.</span><br><span class="line">Submodule <span class="string">'themes/landscape'</span> (https:<span class="comment">//github.com/hexojs/hexo-theme-landscape.git) registered for path 'themes/landscape'</span></span><br><span class="line">Cloning into <span class="string">'D:/code/hexo/blog/themes/landscape'</span>...</span><br><span class="line">remote: Enumerating objects: <span class="number">5</span>, done.</span><br><span class="line">remote: Counting objects: <span class="number">100</span>% (<span class="number">5</span>/<span class="number">5</span>), done.</span><br><span class="line">remote: Compressing objects: <span class="number">100</span>% (<span class="number">5</span>/<span class="number">5</span>), done.</span><br><span class="line">remote: Total <span class="number">846</span> (delta <span class="number">0</span>), reused <span class="number">1</span> (delta <span class="number">0</span>), pack-reused <span class="number">841</span></span><br><span class="line">Receiving objects: <span class="number">100</span>% (<span class="number">846</span>/<span class="number">846</span>), <span class="number">2.55</span> MiB | <span class="number">16.00</span> KiB/s, done.</span><br><span class="line">Resolving deltas: <span class="number">100</span>% (<span class="number">445</span>/<span class="number">445</span>), done.</span><br><span class="line">Submodule path <span class="string">'themes/landscape'</span>: checked out <span class="string">'73a23c51f8487cfcd7c6deec96ccc7543960d350'</span></span><br><span class="line">INFO  Install dependencies</span><br><span class="line">yarn install v1<span class="number">.9</span><span class="number">.4</span></span><br><span class="line">info No lockfile found.</span><br><span class="line">[<span class="number">1</span>/<span class="number">4</span>] Resolving packages...</span><br><span class="line">warning hexo &gt; titlecase@<span class="number">1.1</span><span class="number">.2</span>: no longer maintained</span><br><span class="line">warning hexo &gt; nunjucks &gt; postinstall-build@<span class="number">5.0</span><span class="number">.3</span>: postinstall-build<span class="string">'s behavior is now built into npm! You should migrate off of postinstall-build and use the new `prepare` lifecycle script with npm 5.0.0 or greater.</span></span><br><span class="line"><span class="string">[2/4] Fetching packages...</span></span><br><span class="line"><span class="string">info fsevents@1.2.4: The platform "win32" is incompatible with this module.</span></span><br><span class="line"><span class="string">info "fsevents@1.2.4" is an optional dependency and failed compatibility check. Excluding it from installation.</span></span><br><span class="line"><span class="string">[3/4] Linking dependencies...</span></span><br><span class="line"><span class="string">[4/4] Building fresh packages...</span></span><br><span class="line"><span class="string">success Saved lockfile.</span></span><br><span class="line"><span class="string">Done in 18.06s.</span></span><br><span class="line"><span class="string">INFO  Start blogging with Hexo!</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">$ cd blog</span></span><br><span class="line"><span class="string">hexo的目录结构</span></span><br><span class="line"><span class="string">.</span></span><br><span class="line"><span class="string">├── .deploy       #需要部署的文件</span></span><br><span class="line"><span class="string">├── node_modules  #Hexo插件</span></span><br><span class="line"><span class="string">├── public        #生成的静态网页文件</span></span><br><span class="line"><span class="string">├── scaffolds     #模板</span></span><br><span class="line"><span class="string">├── source        #博客正文和其他源文件, 404 favicon CNAME 等都应该放在这里</span></span><br><span class="line"><span class="string">|   ├── _drafts   #草稿</span></span><br><span class="line"><span class="string">|   └── _posts    #文章</span></span><br><span class="line"><span class="string">├── themes        #主题</span></span><br><span class="line"><span class="string">├── _config.yml   #全局配置文件</span></span><br><span class="line"><span class="string">└── package.json</span></span><br><span class="line"><span class="string">$ npm install</span></span><br><span class="line"><span class="string">npm WARN deprecated titlecase@1.1.2: no longer maintained</span></span><br><span class="line"><span class="string">npm WARN deprecated postinstall-build@5.0.3: postinstall-build'</span>s behavior is now built into npm! You should migrate off <span class="keyword">of</span> postinstall-build and use the <span class="keyword">new</span> <span class="string">`prepare`</span> lifecycle script <span class="keyword">with</span> npm <span class="number">5.0</span><span class="number">.0</span> or greater.</span><br><span class="line"></span><br><span class="line">&gt; nunjucks@<span class="number">3.1</span><span class="number">.4</span> postinstall D:\code\hexo\blog\node_modules\nunjucks</span><br><span class="line">&gt; node postinstall-build.js src</span><br><span class="line"></span><br><span class="line">npm WARN rollback Rolling back node-pre-gyp@<span class="number">0.10</span><span class="number">.0</span> failed (<span class="keyword">this</span> is probably harmless): EPERM: operation not permitted, rmdir <span class="string">'D:\code\hexo\blog\node_modules\fsevents\node_modules'</span></span><br><span class="line">npm notice created a lockfile <span class="keyword">as</span> package-lock.json. You should commit <span class="keyword">this</span> file.</span><br><span class="line">npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@<span class="number">1.2</span><span class="number">.4</span> (node_modules\fsevents):</span><br><span class="line">npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform <span class="keyword">for</span> fsevents@<span class="number">1.2</span><span class="number">.4</span>: wanted &#123;<span class="string">"os"</span>:<span class="string">"darwin"</span>,<span class="string">"arch"</span>:<span class="string">"any"</span>&#125; (current: &#123;<span class="string">"os"</span>:<span class="string">"win32"</span>,<span class="string">"arch"</span>:<span class="string">"x64"</span>&#125;)</span><br><span class="line"></span><br><span class="line">added <span class="number">101</span> packages, removed <span class="number">40</span> packages and updated <span class="number">321</span> packages <span class="keyword">in</span> <span class="number">23.882</span>s</span><br><span class="line"></span><br><span class="line">$ ls</span><br><span class="line">_config.yml    package.json       scaffolds/  themes/</span><br><span class="line">node_modules/  package-lock.json  source/     yarn.lock</span><br><span class="line"></span><br><span class="line">$ hexo g</span><br><span class="line">INFO  Start processing</span><br><span class="line">INFO  Files loaded <span class="keyword">in</span> <span class="number">655</span> ms</span><br><span class="line">INFO  Generated: index.html</span><br><span class="line">INFO  Generated: archives/index.html</span><br><span class="line">INFO  Generated: fancybox/jquery.fancybox.css</span><br><span class="line">INFO  Generated: fancybox/blank.gif</span><br><span class="line">INFO  Generated: fancybox/fancybox_loading@<span class="number">2</span>x.gif</span><br><span class="line">INFO  Generated: fancybox/fancybox_sprite@<span class="number">2</span>x.png</span><br><span class="line">INFO  Generated: fancybox/jquery.fancybox.js</span><br><span class="line">INFO  Generated: fancybox/jquery.fancybox.pack.js</span><br><span class="line">INFO  Generated: fancybox/fancybox_sprite.png</span><br><span class="line">INFO  Generated: fancybox/fancybox_overlay.png</span><br><span class="line">INFO  Generated: archives/<span class="number">2018</span>/<span class="number">11</span>/index.html</span><br><span class="line">INFO  Generated: fancybox/fancybox_loading.gif</span><br><span class="line">INFO  Generated: css/fonts/FontAwesome.otf</span><br><span class="line">INFO  Generated: fancybox/helpers/jquery.fancybox-thumbs.css</span><br><span class="line">INFO  Generated: js/script.js</span><br><span class="line">INFO  Generated: fancybox/helpers/jquery.fancybox-buttons.js</span><br><span class="line">INFO  Generated: fancybox/helpers/jquery.fancybox-media.js</span><br><span class="line">INFO  Generated: fancybox/helpers/jquery.fancybox-buttons.css</span><br><span class="line">INFO  Generated: css/fonts/fontawesome-webfont.eot</span><br><span class="line">INFO  Generated: css/fonts/fontawesome-webfont.woff</span><br><span class="line">INFO  Generated: fancybox/helpers/fancybox_buttons.png</span><br><span class="line">INFO  Generated: fancybox/helpers/jquery.fancybox-thumbs.js</span><br><span class="line">INFO  Generated: css/style.css</span><br><span class="line">INFO  Generated: css/fonts/fontawesome-webfont.ttf</span><br><span class="line">INFO  Generated: archives/<span class="number">2018</span>/index.html</span><br><span class="line">INFO  Generated: css/images/banner.jpg</span><br><span class="line">INFO  Generated: css/fonts/fontawesome-webfont.svg</span><br><span class="line">INFO  Generated: <span class="number">2018</span>/<span class="number">11</span>/<span class="number">20</span>/hello-world/index.html</span><br><span class="line">INFO  <span class="number">28</span> files generated <span class="keyword">in</span> <span class="number">1.26</span> s</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">$ hexo s</span><br><span class="line">INFO  Start processing</span><br><span class="line">INFO  Hexo is running at http:<span class="comment">//localhost:4000 . Press Ctrl+C to stop</span></span><br><span class="line"></span><br><span class="line">$ hexo d</span><br><span class="line">ERROR Deployer not found: git</span><br></pre></td></tr></table></figure>
<h3 id="GitHub-key-配置"><a href="#GitHub-key-配置" class="headerlink" title="GitHub key 配置"></a>GitHub key 配置</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>生成指定名字的密钥 </span><br><span class="line">ssh-keygen -t rsa -C “xx@sina.com” -f ~<span class="regexp">/.ssh/gi</span>thub_sushengbuhuo </span><br><span class="line">会生成 github_sushengbuhuo 和 github_sushengbuhuo.pub 这两个文件</span><br><span class="line"></span><br><span class="line"><span class="number">2.</span>密钥复制到托管平台上 </span><br><span class="line">vim ~<span class="regexp">/.ssh/gi</span>thub_sushengbuhuo.pub ，把内容复制至代码托管平台上</span><br><span class="line"></span><br><span class="line">3.修改config文件 vim ~/.ssh/config #修改config文件，如果没有创建 config</span><br><span class="line"></span><br><span class="line">Host sushengbuhuo.github.com</span><br><span class="line">HostName github.com</span><br><span class="line">User git</span><br><span class="line">IdentityFile ~<span class="regexp">/.ssh/gi</span>thub_sushengbuhuo</span><br><span class="line"></span><br><span class="line">Host github.com</span><br><span class="line">HostName github.com</span><br><span class="line">User git</span><br><span class="line">IdentityFile ~<span class="regexp">/.ssh/gi</span>thub</span><br><span class="line"><span class="number">4.</span>测试验证</span><br><span class="line">$ ssh -T git@github.com:</span><br><span class="line">ssh: Could not resolve hostname github.com:: Name or service not known</span><br><span class="line"></span><br><span class="line">$ ssh -T git@github.com</span><br><span class="line">git@github.com: Permission denied (publickey).</span><br><span class="line"></span><br><span class="line">$ ssh -T  git@sushengbuhuo.github.com</span><br><span class="line">Hi sushengbuhuo! You<span class="string">'ve successfully authenticated, but GitHub does not provide shell access.</span></span><br></pre></td></tr></table></figure>
<h4 id="配置config-yml"><a href="#配置config-yml" class="headerlink" title="配置config.yml"></a>配置config.yml</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">deploy:</span><br><span class="line">  type: git</span><br><span class="line">  repository: git@sushengbuhuo.github.com:sushengbuhuo/sushengbuhuo.github.io.git</span><br><span class="line">  branch: master</span><br><span class="line">  </span><br><span class="line">theme: next</span><br></pre></td></tr></table></figure>
<h3 id="推送到GitHub"><a href="#推送到GitHub" class="headerlink" title="推送到GitHub"></a>推送到GitHub</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br><span class="line">181</span><br><span class="line">182</span><br><span class="line">183</span><br><span class="line">184</span><br><span class="line">185</span><br><span class="line">186</span><br><span class="line">187</span><br><span class="line">188</span><br><span class="line">189</span><br><span class="line">190</span><br><span class="line">191</span><br><span class="line">192</span><br><span class="line">193</span><br><span class="line">194</span><br><span class="line">195</span><br><span class="line">196</span><br><span class="line">197</span><br><span class="line">198</span><br><span class="line">199</span><br><span class="line">200</span><br><span class="line">201</span><br><span class="line">202</span><br><span class="line">203</span><br><span class="line">204</span><br><span class="line">205</span><br><span class="line">206</span><br><span class="line">207</span><br><span class="line">208</span><br><span class="line">209</span><br><span class="line">210</span><br><span class="line">211</span><br><span class="line">212</span><br><span class="line">213</span><br><span class="line">214</span><br><span class="line">215</span><br><span class="line">216</span><br><span class="line">217</span><br><span class="line">218</span><br><span class="line">219</span><br><span class="line">220</span><br><span class="line">221</span><br><span class="line">222</span><br><span class="line">223</span><br><span class="line">224</span><br><span class="line">225</span><br><span class="line">226</span><br></pre></td><td class="code"><pre><span class="line">$ hexo clean &amp;&amp; hexo g</span><br><span class="line">INFO  Deleted database.</span><br><span class="line">INFO  Deleted public folder.</span><br><span class="line">INFO  Start processing</span><br><span class="line">INFO  Files loaded <span class="keyword">in</span> <span class="number">545</span> ms</span><br><span class="line">INFO  Generated: index.html</span><br><span class="line">INFO  Generated: archives/index.html</span><br><span class="line">INFO  Generated: fancybox/fancybox_loading.gif</span><br><span class="line">INFO  Generated: fancybox/fancybox_sprite@<span class="number">2</span>x.png</span><br><span class="line">INFO  Generated: fancybox/jquery.fancybox.js</span><br><span class="line">INFO  Generated: fancybox/fancybox_overlay.png</span><br><span class="line">INFO  Generated: fancybox/jquery.fancybox.css</span><br><span class="line">INFO  Generated: fancybox/jquery.fancybox.pack.js</span><br><span class="line">INFO  Generated: fancybox/blank.gif</span><br><span class="line">INFO  Generated: fancybox/fancybox_loading@<span class="number">2</span>x.gif</span><br><span class="line">INFO  Generated: fancybox/fancybox_sprite.png</span><br><span class="line">INFO  Generated: css/fonts/FontAwesome.otf</span><br><span class="line">INFO  Generated: archives/<span class="number">2018</span>/<span class="number">11</span>/index.html</span><br><span class="line">INFO  Generated: css/fonts/fontawesome-webfont.eot</span><br><span class="line">INFO  Generated: archives/<span class="number">2018</span>/index.html</span><br><span class="line">INFO  Generated: fancybox/helpers/fancybox_buttons.png</span><br><span class="line">INFO  Generated: fancybox/helpers/jquery.fancybox-thumbs.js</span><br><span class="line">INFO  Generated: css/fonts/fontawesome-webfont.woff</span><br><span class="line">INFO  Generated: fancybox/helpers/jquery.fancybox-buttons.css</span><br><span class="line">INFO  Generated: js/script.js</span><br><span class="line">INFO  Generated: fancybox/helpers/jquery.fancybox-buttons.js</span><br><span class="line">INFO  Generated: css/style.css</span><br><span class="line">INFO  Generated: fancybox/helpers/jquery.fancybox-thumbs.css</span><br><span class="line">INFO  Generated: <span class="number">2018</span>/<span class="number">11</span>/<span class="number">20</span>/hello-world/index.html</span><br><span class="line">INFO  Generated: css/fonts/fontawesome-webfont.ttf</span><br><span class="line">INFO  Generated: css/fonts/fontawesome-webfont.svg</span><br><span class="line">INFO  Generated: css/images/banner.jpg</span><br><span class="line">INFO  Generated: fancybox/helpers/jquery.fancybox-media.js</span><br><span class="line">INFO  <span class="number">28</span> files generated <span class="keyword">in</span> <span class="number">1.13</span> s</span><br><span class="line"></span><br><span class="line">$ hexo d</span><br><span class="line">ERROR Deployer not found: git</span><br><span class="line"></span><br><span class="line">$ npm install hexo-deployer-git --save</span><br><span class="line">npm WARN deprecated swig@<span class="number">1.4</span><span class="number">.2</span>: This package is no longer maintained</span><br><span class="line">npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@<span class="number">1.2</span><span class="number">.4</span> (node_modules\fsevents):</span><br><span class="line">npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform <span class="keyword">for</span> fsevents@<span class="number">1.2</span><span class="number">.4</span>: wanted &#123;<span class="string">"os"</span>:<span class="string">"darwin"</span>,<span class="string">"arch"</span>:<span class="string">"any"</span>&#125; (current: &#123;<span class="string">"os"</span>:<span class="string">"win32"</span>,<span class="string">"arch"</span>:<span class="string">"x64"</span>&#125;)</span><br><span class="line"></span><br><span class="line">+ hexo-deployer-git@<span class="number">0.3</span><span class="number">.1</span></span><br><span class="line">added <span class="number">31</span> packages <span class="keyword">in</span> <span class="number">17.866</span>s</span><br><span class="line"></span><br><span class="line">$ hexo d</span><br><span class="line">INFO  Deploying: git</span><br><span class="line">INFO  Setting up Git deployment...</span><br><span class="line">Initialized empty Git repository <span class="keyword">in</span> D:<span class="regexp">/code/</span>hexo/blog/.deploy_git/.git/</span><br><span class="line">[master (root-commit) <span class="number">9</span>c86786] First commit</span><br><span class="line"> Committer: unknown &lt;xxx@sina.com.cn&gt;</span><br><span class="line">Your name and email address were configured automatically based</span><br><span class="line">on your username and hostname. Please check that they are accurate.</span><br><span class="line">You can suppress <span class="keyword">this</span> message by setting them explicitly:</span><br><span class="line"></span><br><span class="line">    git config --global user.name <span class="string">"Your Name"</span></span><br><span class="line">    git config --global user.email you@example.com</span><br><span class="line"></span><br><span class="line">After doing <span class="keyword">this</span>, you may fix the identity used <span class="keyword">for</span> <span class="keyword">this</span> commit <span class="keyword">with</span>:</span><br><span class="line"></span><br><span class="line">    git commit --amend --reset-author</span><br><span class="line"></span><br><span class="line"> <span class="number">1</span> file changed, <span class="number">0</span> insertions(+), <span class="number">0</span> deletions(-)</span><br><span class="line"> create mode <span class="number">100644</span> placeholder</span><br><span class="line">INFO  Clearing .deploy_git folder...</span><br><span class="line">INFO  Copying files <span class="keyword">from</span> public folder...</span><br><span class="line">INFO  Copying files <span class="keyword">from</span> extend dirs...</span><br><span class="line">warning: LF will be replaced by CRLF <span class="keyword">in</span> <span class="number">2018</span>/<span class="number">11</span>/<span class="number">20</span>/hello-world/index.html.</span><br><span class="line">The file will have its original line endings <span class="keyword">in</span> your working directory.</span><br><span class="line">warning: LF will be replaced by CRLF <span class="keyword">in</span> archives/<span class="number">2018</span>/<span class="number">11</span>/index.html.</span><br><span class="line">The file will have its original line endings <span class="keyword">in</span> your working directory.</span><br><span class="line">warning: LF will be replaced by CRLF <span class="keyword">in</span> archives/<span class="number">2018</span>/index.html.</span><br><span class="line">The file will have its original line endings <span class="keyword">in</span> your working directory.</span><br><span class="line">warning: LF will be replaced by CRLF <span class="keyword">in</span> archives/index.html.</span><br><span class="line">The file will have its original line endings <span class="keyword">in</span> your working directory.</span><br><span class="line">warning: LF will be replaced by CRLF <span class="keyword">in</span> css/style.css.</span><br><span class="line">The file will have its original line endings <span class="keyword">in</span> your working directory.</span><br><span class="line">warning: LF will be replaced by CRLF <span class="keyword">in</span> fancybox/helpers/jquery.fancybox-buttons.css.</span><br><span class="line">The file will have its original line endings <span class="keyword">in</span> your working directory.</span><br><span class="line">warning: LF will be replaced by CRLF <span class="keyword">in</span> fancybox/helpers/jquery.fancybox-buttons.js.</span><br><span class="line">The file will have its original line endings <span class="keyword">in</span> your working directory.</span><br><span class="line">warning: LF will be replaced by CRLF <span class="keyword">in</span> fancybox/helpers/jquery.fancybox-media.js.</span><br><span class="line">The file will have its original line endings <span class="keyword">in</span> your working directory.</span><br><span class="line">warning: LF will be replaced by CRLF <span class="keyword">in</span> fancybox/helpers/jquery.fancybox-thumbs.css.</span><br><span class="line">The file will have its original line endings <span class="keyword">in</span> your working directory.</span><br><span class="line">warning: LF will be replaced by CRLF <span class="keyword">in</span> fancybox/helpers/jquery.fancybox-thumbs.js.</span><br><span class="line">The file will have its original line endings <span class="keyword">in</span> your working directory.</span><br><span class="line">warning: LF will be replaced by CRLF <span class="keyword">in</span> fancybox/jquery.fancybox.css.</span><br><span class="line">The file will have its original line endings <span class="keyword">in</span> your working directory.</span><br><span class="line">warning: LF will be replaced by CRLF <span class="keyword">in</span> fancybox/jquery.fancybox.js.</span><br><span class="line">The file will have its original line endings <span class="keyword">in</span> your working directory.</span><br><span class="line">warning: LF will be replaced by CRLF <span class="keyword">in</span> fancybox/jquery.fancybox.pack.js.</span><br><span class="line">The file will have its original line endings <span class="keyword">in</span> your working directory.</span><br><span class="line">warning: LF will be replaced by CRLF <span class="keyword">in</span> index.html.</span><br><span class="line">The file will have its original line endings <span class="keyword">in</span> your working directory.</span><br><span class="line">warning: LF will be replaced by CRLF <span class="keyword">in</span> js/script.js.</span><br><span class="line">The file will have its original line endings <span class="keyword">in</span> your working directory.</span><br><span class="line">[master b7f7580] Site updated: <span class="number">2018</span><span class="number">-11</span><span class="number">-20</span> <span class="number">11</span>:<span class="number">51</span>:<span class="number">50</span></span><br><span class="line"> Committer: unknown &lt;xxx@sina.com.cn&gt;</span><br><span class="line">Your name and email address were configured automatically based</span><br><span class="line">on your username and hostname. Please check that they are accurate.</span><br><span class="line">You can suppress <span class="keyword">this</span> message by setting them explicitly:</span><br><span class="line"></span><br><span class="line">    git config --global user.name <span class="string">"Your Name"</span></span><br><span class="line">    git config --global user.email you@example.com</span><br><span class="line"></span><br><span class="line">After doing <span class="keyword">this</span>, you may fix the identity used <span class="keyword">for</span> <span class="keyword">this</span> commit <span class="keyword">with</span>:</span><br><span class="line"></span><br><span class="line">    git commit --amend --reset-author</span><br><span class="line"></span><br><span class="line"> <span class="number">29</span> files changed, <span class="number">5777</span> insertions(+)</span><br><span class="line"> create mode <span class="number">100644</span> <span class="number">2018</span>/<span class="number">11</span>/<span class="number">20</span>/hello-world/index.html</span><br><span class="line"> create mode <span class="number">100644</span> archives/<span class="number">2018</span>/<span class="number">11</span>/index.html</span><br><span class="line"> create mode <span class="number">100644</span> archives/<span class="number">2018</span>/index.html</span><br><span class="line"> create mode <span class="number">100644</span> archives/index.html</span><br><span class="line"> create mode <span class="number">100644</span> css/fonts/FontAwesome.otf</span><br><span class="line"> create mode <span class="number">100644</span> css/fonts/fontawesome-webfont.eot</span><br><span class="line"> create mode <span class="number">100644</span> css/fonts/fontawesome-webfont.svg</span><br><span class="line"> create mode <span class="number">100644</span> css/fonts/fontawesome-webfont.ttf</span><br><span class="line"> create mode <span class="number">100644</span> css/fonts/fontawesome-webfont.woff</span><br><span class="line"> create mode <span class="number">100644</span> css/images/banner.jpg</span><br><span class="line"> create mode <span class="number">100644</span> css/style.css</span><br><span class="line"> create mode <span class="number">100644</span> fancybox/blank.gif</span><br><span class="line"> create mode <span class="number">100644</span> fancybox/fancybox_loading.gif</span><br><span class="line"> create mode <span class="number">100644</span> fancybox/fancybox_loading@<span class="number">2</span>x.gif</span><br><span class="line"> create mode <span class="number">100644</span> fancybox/fancybox_overlay.png</span><br><span class="line"> create mode <span class="number">100644</span> fancybox/fancybox_sprite.png</span><br><span class="line"> create mode <span class="number">100644</span> fancybox/fancybox_sprite@<span class="number">2</span>x.png</span><br><span class="line"> create mode <span class="number">100644</span> fancybox/helpers/fancybox_buttons.png</span><br><span class="line"> create mode <span class="number">100644</span> fancybox/helpers/jquery.fancybox-buttons.css</span><br><span class="line"> create mode <span class="number">100644</span> fancybox/helpers/jquery.fancybox-buttons.js</span><br><span class="line"> create mode <span class="number">100644</span> fancybox/helpers/jquery.fancybox-media.js</span><br><span class="line"> create mode <span class="number">100644</span> fancybox/helpers/jquery.fancybox-thumbs.css</span><br><span class="line"> create mode <span class="number">100644</span> fancybox/helpers/jquery.fancybox-thumbs.js</span><br><span class="line"> create mode <span class="number">100644</span> fancybox/jquery.fancybox.css</span><br><span class="line"> create mode <span class="number">100644</span> fancybox/jquery.fancybox.js</span><br><span class="line"> create mode <span class="number">100644</span> fancybox/jquery.fancybox.pack.js</span><br><span class="line"> create mode <span class="number">100644</span> index.html</span><br><span class="line"> create mode <span class="number">100644</span> js/script.js</span><br><span class="line"> <span class="keyword">delete</span> mode <span class="number">100644</span> placeholder</span><br><span class="line">git@github.com: Permission denied (publickey).</span><br><span class="line">fatal: Could not read <span class="keyword">from</span> remote repository.</span><br><span class="line"></span><br><span class="line">Please make sure you have the correct access rights</span><br><span class="line">and the repository exists.</span><br><span class="line">FATAL Something<span class="string">'s wrong. Maybe you can find the solution here: http://hexo.io/docs/troubleshooting.html</span></span><br><span class="line"><span class="string">Error: git@github.com: Permission denied (publickey).</span></span><br><span class="line"><span class="string">fatal: Could not read from remote repository.</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">Please make sure you have the correct access rights</span></span><br><span class="line"><span class="string">and the repository exists.</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">    at ChildProcess.&lt;anonymous&gt; (D:\code\hexo\blog\node_modules\hexo-util\lib\spawn.js:37:17)</span></span><br><span class="line"><span class="string">    at emitTwo (events.js:126:13)</span></span><br><span class="line"><span class="string">    at ChildProcess.emit (events.js:214:7)</span></span><br><span class="line"><span class="string">    at ChildProcess.cp.emit (D:\code\hexo\blog\node_modules\cross-spawn\lib\enoent.js:40:29)</span></span><br><span class="line"><span class="string">    at maybeClose (internal/child_process.js:925:16)</span></span><br><span class="line"><span class="string">    at Socket.stream.socket.on (internal/child_process.js:346:11)</span></span><br><span class="line"><span class="string">    at emitOne (events.js:116:13)</span></span><br><span class="line"><span class="string">    at Socket.emit (events.js:211:7)</span></span><br><span class="line"><span class="string">    at Pipe._handle.close [as _onclose] (net.js:557:12)</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">$ hexo d</span></span><br><span class="line"><span class="string">INFO  Deploying: git</span></span><br><span class="line"><span class="string">INFO  Clearing .deploy_git folder...</span></span><br><span class="line"><span class="string">INFO  Copying files from public folder...</span></span><br><span class="line"><span class="string">INFO  Copying files from extend dirs...</span></span><br><span class="line"><span class="string">warning: LF will be replaced by CRLF in 2018/11/20/hello-world/index.html.</span></span><br><span class="line"><span class="string">The file will have its original line endings in your working directory.</span></span><br><span class="line"><span class="string">warning: LF will be replaced by CRLF in archives/2018/11/index.html.</span></span><br><span class="line"><span class="string">The file will have its original line endings in your working directory.</span></span><br><span class="line"><span class="string">warning: LF will be replaced by CRLF in archives/2018/index.html.</span></span><br><span class="line"><span class="string">The file will have its original line endings in your working directory.</span></span><br><span class="line"><span class="string">warning: LF will be replaced by CRLF in archives/index.html.</span></span><br><span class="line"><span class="string">The file will have its original line endings in your working directory.</span></span><br><span class="line"><span class="string">warning: LF will be replaced by CRLF in css/style.css.</span></span><br><span class="line"><span class="string">The file will have its original line endings in your working directory.</span></span><br><span class="line"><span class="string">warning: LF will be replaced by CRLF in fancybox/helpers/jquery.fancybox-buttons.css.</span></span><br><span class="line"><span class="string">The file will have its original line endings in your working directory.</span></span><br><span class="line"><span class="string">warning: LF will be replaced by CRLF in fancybox/helpers/jquery.fancybox-buttons.js.</span></span><br><span class="line"><span class="string">The file will have its original line endings in your working directory.</span></span><br><span class="line"><span class="string">warning: LF will be replaced by CRLF in fancybox/helpers/jquery.fancybox-media.js.</span></span><br><span class="line"><span class="string">The file will have its original line endings in your working directory.</span></span><br><span class="line"><span class="string">warning: LF will be replaced by CRLF in fancybox/helpers/jquery.fancybox-thumbs.css.</span></span><br><span class="line"><span class="string">The file will have its original line endings in your working directory.</span></span><br><span class="line"><span class="string">warning: LF will be replaced by CRLF in fancybox/helpers/jquery.fancybox-thumbs.js.</span></span><br><span class="line"><span class="string">The file will have its original line endings in your working directory.</span></span><br><span class="line"><span class="string">warning: LF will be replaced by CRLF in fancybox/jquery.fancybox.css.</span></span><br><span class="line"><span class="string">The file will have its original line endings in your working directory.</span></span><br><span class="line"><span class="string">warning: LF will be replaced by CRLF in fancybox/jquery.fancybox.js.</span></span><br><span class="line"><span class="string">The file will have its original line endings in your working directory.</span></span><br><span class="line"><span class="string">warning: LF will be replaced by CRLF in fancybox/jquery.fancybox.pack.js.</span></span><br><span class="line"><span class="string">The file will have its original line endings in your working directory.</span></span><br><span class="line"><span class="string">warning: LF will be replaced by CRLF in js/script.js.</span></span><br><span class="line"><span class="string">The file will have its original line endings in your working directory.</span></span><br><span class="line"><span class="string">On branch master</span></span><br><span class="line"><span class="string">nothing to commit, working tree clean</span></span><br><span class="line"><span class="string">Branch '</span>master<span class="string">' set up to track remote branch '</span>master<span class="string">' from '</span>git@sushengbuhuo.github.com:sushengbuhuo/sushengbuhuo.github.io.git<span class="string">'.</span></span><br><span class="line"><span class="string">To sushengbuhuo.github.com:sushengbuhuo/sushengbuhuo.github.io.git</span></span><br><span class="line"><span class="string"> + 3037877...b7f7580 HEAD -&gt; master (forced update)</span></span><br><span class="line"><span class="string">INFO  Deploy done: git</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">$ git clone https://github.com/iissnan/hexo-theme-next themes/next</span></span><br><span class="line"><span class="string">Cloning into '</span>themes/next<span class="string">'...</span></span><br><span class="line"><span class="string">remote: Enumerating objects: 12033, done.</span></span><br><span class="line"><span class="string">remote: Total 12033 (delta 0), reused 0 (delta 0), pack-reused 12033</span></span><br><span class="line"><span class="string">Receiving objects: 100% (12033/12033), 12.95 MiB | 79.00 KiB/s, done.</span></span><br><span class="line"><span class="string">Resolving deltas: 100% (6966/6966), done.</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">$ hexo clean &amp;&amp; hexo g</span></span><br><span class="line"><span class="string">INFO  Deleted database.</span></span><br><span class="line"><span class="string">INFO  Deleted public folder.</span></span><br><span class="line"><span class="string">INFO  Start processing</span></span><br><span class="line"><span class="string">$ hexo d</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">$ hexo s</span></span><br><span class="line"><span class="string">INFO  Start processing</span></span><br><span class="line"><span class="string">WARN  ===============================================================</span></span><br><span class="line"><span class="string">WARN  ========================= ATTENTION! ==========================</span></span><br><span class="line"><span class="string">WARN  ===============================================================</span></span><br><span class="line"><span class="string">WARN   NexT repository is moving here: https://github.com/theme-next</span></span><br><span class="line"><span class="string">WARN  ===============================================================</span></span><br><span class="line"><span class="string">WARN   It'</span>s rebase to v6<span class="number">.0</span><span class="number">.0</span> and future maintenance will resume there</span><br><span class="line">WARN  ===============================================================</span><br><span class="line">INFO  Hexo is running at http:<span class="comment">//localhost:4000 . Press Ctrl+C to stop</span></span><br></pre></td></tr></table></figure>
<h3 id="安装插件"><a href="#安装插件" class="headerlink" title="安装插件"></a>安装插件</h3><p>登录<a href="http://localhost:4000/admin" target="_blank" rel="noopener">admin</a> 即可看到我们所有的文章内容<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br></pre></td><td class="code"><pre><span class="line">λ npm i hexo-admin --save</span><br><span class="line">npm WARN deprecated minimatch@<span class="number">2.0</span><span class="number">.10</span>: Please update to minimatch <span class="number">3.0</span><span class="number">.2</span> or higher to avoid a <span class="built_in">RegExp</span> DoS issue</span><br><span class="line">npm WARN deprecated connect@<span class="number">2.7</span><span class="number">.11</span>: connect <span class="number">2.</span>x series is deprecated</span><br><span class="line">npm WARN deprecated cryptiles@<span class="number">2.0</span><span class="number">.5</span>: This version is no longer maintained. Please upgrade to the latest version.</span><br><span class="line">npm WARN deprecated boom@<span class="number">2.10</span><span class="number">.1</span>: This version is no longer maintained. Please upgrade to the latest version.</span><br><span class="line">npm WARN deprecated hoek@<span class="number">2.16</span><span class="number">.3</span>: This version is no longer maintained. Please upgrade to the latest version.</span><br><span class="line">npm WARN acorn-dynamic-<span class="keyword">import</span>@<span class="number">4.0</span><span class="number">.0</span> requires a peer <span class="keyword">of</span> acorn@^<span class="number">6.0</span><span class="number">.0</span> but none is installed. You must install peer dependencies yourself.</span><br><span class="line">npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@<span class="number">1.2</span><span class="number">.4</span> (node_modules\fsevents):</span><br><span class="line">npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform <span class="keyword">for</span> fsevents@<span class="number">1.2</span><span class="number">.4</span>: wanted &#123;<span class="string">"os"</span>:<span class="string">"darwin"</span>,<span class="string">"arch"</span>:<span class="string">"any"</span>&#125; (current: &#123;<span class="string">"os"</span>:<span class="string">"win32"</span>,<span class="string">"arch"</span>:<span class="string">"x64"</span>&#125;)</span><br><span class="line"></span><br><span class="line">+ hexo-admin@<span class="number">2.3</span><span class="number">.0</span></span><br><span class="line">added <span class="number">251</span> packages <span class="keyword">in</span> <span class="number">23.975</span>s</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">   ╭─────────────────────────────────────╮</span><br><span class="line">   │                                     │</span><br><span class="line">   │   Update available <span class="number">5.6</span><span class="number">.0</span> → <span class="number">6.4</span><span class="number">.1</span>    │</span><br><span class="line">   │       Run npm i npm to update       │</span><br><span class="line">   │                                     │</span><br><span class="line">   ╰─────────────────────────────────────╯</span><br><span class="line"></span><br><span class="line">#网站底部字数统计</span><br><span class="line">d:\code\hexo\blog</span><br><span class="line">λ npm install hexo-wordcount --save</span><br><span class="line">npm WARN rollback Rolling back node-pre-gyp@<span class="number">0.10</span><span class="number">.0</span> failed (<span class="keyword">this</span> is probably harmless): EPERM: operation not permitted, scandir <span class="string">'d:\code\hexo\blog\node_modules\fsevents\node_modules'</span></span><br><span class="line">npm WARN acorn-dynamic-<span class="keyword">import</span>@<span class="number">4.0</span><span class="number">.0</span> requires a peer <span class="keyword">of</span> acorn@^<span class="number">6.0</span><span class="number">.0</span> but none is installed. You must install peer dependencies yourself.</span><br><span class="line">npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@<span class="number">1.2</span><span class="number">.4</span> (node_modules\fsevents):</span><br><span class="line">npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform <span class="keyword">for</span> fsevents@<span class="number">1.2</span><span class="number">.4</span>: wanted &#123;<span class="string">"os"</span>:<span class="string">"darwin"</span>,<span class="string">"arch"</span>:<span class="string">"any"</span>&#125; (current: &#123;<span class="string">"os"</span>:<span class="string">"win32"</span>,<span class="string">"arch"</span>:<span class="string">"x64"</span>&#125;)</span><br><span class="line"></span><br><span class="line">+ hexo-wordcount@<span class="number">6.0</span><span class="number">.1</span></span><br><span class="line">added <span class="number">1</span> package <span class="keyword">in</span> <span class="number">10.289</span>s</span><br><span class="line"></span><br><span class="line">搜索npm install hexo-generator-searchdb --save </span><br><span class="line">npm install hexo-generator-search  --save</span><br><span class="line"></span><br><span class="line">在根目录下的/theme/next/_config.yml文件中添加配置：</span><br><span class="line">search:</span><br><span class="line">  path: search.xml</span><br><span class="line">  field: post</span><br><span class="line">  format: html</span><br><span class="line">  limit: <span class="number">10000</span></span><br><span class="line">  </span><br><span class="line"> 在根目录下的/theme/next/_config.yml文件中搜索local_search，将enable改为<span class="literal">true</span>：</span><br><span class="line"> local_search:</span><br><span class="line">   enable: <span class="literal">true</span></span><br></pre></td></tr></table></figure></p>
<h3 id="问题"><a href="#问题" class="headerlink" title="问题"></a>问题</h3><p>中文乱码</p>
<p>将config.yml 和md文件编码转为utf-8</p>
<p>修改config.yml <code>language: zh-Hans</code></p>
<h3 id="新建文章"><a href="#新建文章" class="headerlink" title="新建文章"></a>新建文章</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ hexo new <span class="string">"PHP依赖注入"</span></span><br></pre></td></tr></table></figure>
<p>Hexo 默认以标题做为文件名称，但您可编辑_config.yml <code>new_post_name</code> 参数来改变默认的文件名称，举例来说，设为 :year-:month-:day-:title.md 可让您更方便的通过日期来管理文章。</p>
<h3 id="标签分类"><a href="#标签分类" class="headerlink" title="标签分类"></a>标签分类</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br></pre></td><td class="code"><pre><span class="line">主菜单设置 blog/_config.yml 添加如下配置</span><br><span class="line">menu:</span><br><span class="line">  home: <span class="regexp">/</span></span><br><span class="line"><span class="regexp">  archives: /</span>archives</span><br><span class="line">  categories: <span class="regexp">/categories   #手动新建</span></span><br><span class="line">  tags: /tags               #手动新建</span><br><span class="line">  commonweal: <span class="regexp">/404.html     #手动新建</span></span><br><span class="line">  about: /about             #手动新建</span><br><span class="line"> </span><br><span class="line"> </span><br><span class="line">  next/_config.yml 添加如下配置</span><br><span class="line">menu:</span><br><span class="line">  home: <span class="regexp">/ || home</span></span><br><span class="line"><span class="regexp">  about: /</span>about/ || user</span><br><span class="line">  tags: <span class="regexp">/tags/</span> || tags</span><br><span class="line">  categories: <span class="regexp">/categories/</span> || th</span><br><span class="line">  archives: <span class="regexp">/archives/</span> || archive</span><br><span class="line">  </span><br><span class="line">  </span><br><span class="line">标签云 页面</span><br><span class="line">命令</span><br><span class="line"></span><br><span class="line">hexo <span class="keyword">new</span> page tags</span><br><span class="line">页面设置</span><br><span class="line"></span><br><span class="line">title: tags</span><br><span class="line">date: <span class="number">2015</span><span class="number">-09</span><span class="number">-19</span> <span class="number">22</span>:<span class="number">37</span>:<span class="number">08</span></span><br><span class="line">type: <span class="string">"tags"</span></span><br><span class="line">comments: <span class="literal">false</span></span><br><span class="line">---</span><br><span class="line">关于 页面</span><br><span class="line">命令</span><br><span class="line"></span><br><span class="line">hexo <span class="keyword">new</span> page about</span><br><span class="line">页面设置</span><br><span class="line"></span><br><span class="line">title: about</span><br><span class="line">date: <span class="number">2015</span><span class="number">-09</span><span class="number">-19</span> <span class="number">22</span>:<span class="number">37</span>:<span class="number">08</span></span><br><span class="line">comments: <span class="literal">false</span></span><br><span class="line">---</span><br><span class="line">About Me #这里编辑 '关于我' 的内容</span><br><span class="line">分类 页面</span><br><span class="line">命令</span><br><span class="line"></span><br><span class="line">hexo <span class="keyword">new</span> page categories</span><br><span class="line">页面设置</span><br><span class="line"></span><br><span class="line">title: categories</span><br><span class="line">date: <span class="number">2015</span><span class="number">-09</span><span class="number">-19</span> <span class="number">22</span>:<span class="number">37</span>:<span class="number">08</span></span><br><span class="line">type: <span class="string">"categories"</span></span><br><span class="line">comments: <span class="literal">false</span></span><br><span class="line">---</span><br></pre></td></tr></table></figure>
<h3 id="显示统计字数和估计阅读时长"><a href="#显示统计字数和估计阅读时长" class="headerlink" title="显示统计字数和估计阅读时长"></a>显示统计字数和估计阅读时长</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">npm install hexo-wordcount --save</span><br><span class="line">vi blog\themes\hexo-theme-next\_config.yml</span><br><span class="line">post_wordcount:</span><br><span class="line">  item_text: <span class="literal">true</span></span><br><span class="line">  wordcount: <span class="literal">true</span></span><br><span class="line">  min2read: <span class="literal">true</span></span><br><span class="line">  totalcount: <span class="literal">false</span></span><br><span class="line">  separated_meta: <span class="literal">false</span></span><br><span class="line">vi blog\themes\hexo-theme-next\layout\_macro\ **post.swig**文件</span><br><span class="line"> &#123;&#123; wordcount(post.content) &#125;&#125; 字</span><br><span class="line">   &#123;&#123; min2read(post.content) &#125;&#125; 分钟</span><br></pre></td></tr></table></figure>
<h3 id="显示总访客数和总浏览量"><a href="#显示总访客数和总浏览量" class="headerlink" title="显示总访客数和总浏览量"></a>显示总访客数和总浏览量</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br></pre></td><td class="code"><pre><span class="line">vi  D:\blog\themes\hexo-theme-next\layout_partials\ footer.swig</span><br><span class="line">首行添加如下代码：</span><br><span class="line">&lt;script <span class="keyword">async</span> src=<span class="string">"https://dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js"</span>&gt;&lt;/script&gt;</span><br><span class="line"></span><br><span class="line">#接着修改相应代码：</span><br><span class="line"># 添加总访客量</span><br><span class="line">&lt;span id=<span class="string">"busuanzi_container_site_uv"</span>&gt;</span><br><span class="line">  访客数:&lt;span id="busuanzi_value_site_uv"&gt;&lt;/span&gt;人次</span><br><span class="line">&lt;<span class="regexp">/span&gt;</span></span><br><span class="line"><span class="regexp"></span></span><br><span class="line"><span class="regexp">&#123;% if theme.footer.powered %&#125;</span></span><br><span class="line"><span class="regexp">  &lt;!--&lt;div class="powered-by"&gt;&#123;#</span></span><br><span class="line"><span class="regexp">  #&#125;&#123;&#123; __('footer.powered', '&lt;a class="theme-link" target="_blank" href="https:/</span><span class="regexp">/hexo.io"&gt;Hexo&lt;/</span>a&gt;<span class="string">') &#125;&#125;&#123;#</span></span><br><span class="line"><span class="string">#&#125;&lt;/div&gt;--&gt;</span></span><br><span class="line"><span class="string">&#123;% endif %&#125;</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string"># 添加'</span>|<span class="string">'符号</span></span><br><span class="line"><span class="string">&#123;% if theme.footer.powered and theme.footer.theme.enable %&#125;</span></span><br><span class="line"><span class="string">  &lt;span class="post-meta-divider"&gt;|&lt;/span&gt;</span></span><br><span class="line"><span class="string">&#123;% endif %&#125;</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">&#123;% if theme.footer.custom_text %&#125;</span></span><br><span class="line"><span class="string">  &lt;div class="footer-custom"&gt;&#123;#</span></span><br><span class="line"><span class="string">  #&#125;&#123;&#123; theme.footer.custom_text &#125;&#125;&#123;#</span></span><br><span class="line"><span class="string">#&#125;&lt;/div&gt;</span></span><br><span class="line"><span class="string">&#123;% endif %&#125;</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string"># 添加总访问量</span></span><br><span class="line"><span class="string">&lt;span id="busuanzi_container_site_pv"&gt;</span></span><br><span class="line"><span class="string">   总访问量:&lt;span id="busuanzi_value_site_pv"&gt;&lt;/span&gt;次</span></span><br><span class="line"><span class="string">&lt;/span&gt;</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string"># 添加'</span>|<span class="string">'符号</span></span><br><span class="line"><span class="string">&#123;% if theme.footer.powered and theme.footer.theme.enable %&#125;</span></span><br><span class="line"><span class="string">  &lt;span class="post-meta-divider"&gt;|&lt;/span&gt;</span></span><br><span class="line"><span class="string">&#123;% endif %&#125;</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string"># 添加博客全站共：</span></span><br><span class="line"><span class="string">&lt;div class="theme-info"&gt;</span></span><br><span class="line"><span class="string">  &lt;div class="powered-by"&gt;&lt;/div&gt;</span></span><br><span class="line"><span class="string">  &lt;span class="post-count"&gt;博客全站共&#123;&#123; totalcount(site) &#125;&#125;字&lt;/span&gt;</span></span><br><span class="line"><span class="string">&lt;/div&gt;</span></span><br></pre></td></tr></table></figure>
<h3 id="备份文章"><a href="#备份文章" class="headerlink" title="备份文章"></a>备份文章</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br></pre></td><td class="code"><pre><span class="line">将themes/next/(我用的是NexT主题)中的.git/删除，否则无法将主题文件夹push；</span><br><span class="line">在本地blog文件夹下创建文件.gitignore(一般都自带一个)，打开后写入</span><br><span class="line"> </span><br><span class="line"> https:<span class="comment">//yfzhou.coding.me/2018/09/17/Hexo-Next%E6%90%AD%E5%BB%BA%E4%B8%AA%E4%BA%BA%E5%8D%9A%E5%AE%A2%EF%BC%88Hexo%E5%8D%9A%E5%AE%A2%E5%A4%87%E4%BB%BD%EF%BC%89/</span></span><br><span class="line">.DS_Store</span><br><span class="line">Thumbs.db</span><br><span class="line">db.json</span><br><span class="line">*.log</span><br><span class="line">node_modules/</span><br><span class="line">public/</span><br><span class="line">.deploy*<span class="regexp">/</span></span><br><span class="line"><span class="regexp">在本地blog文件夹下执行命令</span></span><br><span class="line"><span class="regexp">#git初始化</span></span><br><span class="line"><span class="regexp">git init</span></span><br><span class="line"><span class="regexp">#创建hexo分支，用来存放源码</span></span><br><span class="line"><span class="regexp">git checkout -b hexo</span></span><br><span class="line"><span class="regexp">#git 文件添加</span></span><br><span class="line"><span class="regexp">git add .</span></span><br><span class="line"><span class="regexp">#git 提交</span></span><br><span class="line"><span class="regexp">git commit -m "init"</span></span><br><span class="line"><span class="regexp">#添加远程仓库</span></span><br><span class="line"><span class="regexp">git remote add origin git@github.com:sushengbuhuo/</span>sushengbuhuo.github.io.git</span><br><span class="line">#push到hexo分支</span><br><span class="line">git push origin hexo</span><br><span class="line">执行hexo d -g生成网站并部署到GitHub上</span><br><span class="line"></span><br><span class="line">这样一来，在GitHub上的git@github.com:sushengbuhuo/sushengbuhuo.github.io.git仓库就有两个分支，</span><br><span class="line">一个hexo分支用来存放网站的原始文件，一个master分支用来存放生成的静态网页。</span><br><span class="line"></span><br><span class="line">当重装电脑之后，或者想在其他电脑上修改博客，可以使用下列步骤：</span><br><span class="line"></span><br><span class="line"><span class="number">1</span>、先安装hexo</span><br><span class="line">$ npm install -g hexo-cli</span><br><span class="line"><span class="number">2</span>、存在github上的git clone下来</span><br><span class="line">git clone git@github.com:sushengbuhuo/sushengbuhuo.github.io.git</span><br><span class="line"><span class="number">3</span>、项目文件夹下npm</span><br><span class="line">cd项目名/ </span><br><span class="line">npm install –no-bin-links</span><br><span class="line">$ npm install hexo-deployer-git</span><br><span class="line"><span class="number">4</span>、重新配置github和coding的公钥</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">每次写作之后,可以使用下列步骤：</span><br><span class="line"></span><br><span class="line">hexo d#生成网站并部署到GitHub上</span><br><span class="line">git add .</span><br><span class="line">git commit -m <span class="string">'update'</span></span><br><span class="line">git push origin hexo</span><br></pre></td></tr></table></figure>
<h3 id="添加gitter在线交流"><a href="#添加gitter在线交流" class="headerlink" title="添加gitter在线交流"></a>添加gitter在线交流</h3><p>用GitHub登录 <a href="https://gitter.im" target="_blank" rel="noopener">https://gitter.im</a><br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">vi next/layout/_third-party/gitter.swig</span><br><span class="line"></span><br><span class="line">&lt;script&gt;</span><br><span class="line">  ((<span class="built_in">window</span>.gitter = &#123;&#125;).chat = &#123;&#125;).options = &#123;</span><br><span class="line">    <span class="comment">//room替换成自己的聊天室名称即可，room的名称规则是：username/roomname</span></span><br><span class="line">    <span class="comment">//http://www.xetlab.com/2019/04/14/%E7%BB%99%E5%9F%BA%E4%BA%8EHEXO%E7%9A%84%E5%8D%9A%E5%AE%A2%E6%B7%BB%E5%8A%A0gitter%E5%9C%A8%E7%BA%BF%E4%BA%A4%E6%B5%81/</span></span><br><span class="line">    room: <span class="string">'sushengbuhuo/chat'</span></span><br><span class="line">  &#125;;</span><br><span class="line">&lt;<span class="regexp">/script&gt;</span></span><br><span class="line"><span class="regexp">&lt;script src="https:/</span><span class="regexp">/sidecar.gitter.im/</span>dist/sidecar.v1.js<span class="string">" async defer&gt;&lt;/script&gt;</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">vi next/layout/layout/_layout.swig</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">&#123;% include '_third-party/gitter.swig' %&#125;</span></span><br></pre></td></tr></table></figure></p>
<h3 id="增加评论utterance"><a href="#增加评论utterance" class="headerlink" title="增加评论utterance"></a>增加评论utterance</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br></pre></td><td class="code"><pre><span class="line">https:<span class="comment">//www.njphper.com/posts/a4cd94b2.html</span></span><br><span class="line">通过https:<span class="comment">//github.com/apps/utterances 用GitHub登录，然后设置一个公开目录接受评论。比如 sushengbuhuo/laravel_ioc_demo</span></span><br><span class="line">&lt;script src=<span class="string">"https://utteranc.es/client.js"</span></span><br><span class="line">        repo=<span class="string">"sushengbuhuo/laravel_ioc_demo"</span></span><br><span class="line">        issue-term=<span class="string">"pathname"</span></span><br><span class="line">        label=<span class="string">"susheng"</span></span><br><span class="line">        theme=<span class="string">"github-light"</span></span><br><span class="line">        crossorigin=<span class="string">"anonymous"</span></span><br><span class="line">        <span class="keyword">async</span>&gt;</span><br><span class="line">&lt;<span class="regexp">/script&gt;</span></span><br><span class="line"><span class="regexp"></span></span><br><span class="line"><span class="regexp">vi _confgi.yml 增加</span></span><br><span class="line"><span class="regexp">utterance:</span></span><br><span class="line"><span class="regexp">  enable: true</span></span><br><span class="line"><span class="regexp">  repo: sushengbuhuo/</span>laravel_ioc_demo</span><br><span class="line">  theme: github-light</span><br><span class="line">  issue-term: pathname</span><br><span class="line">评论主题的 theme 选项如下:</span><br><span class="line"></span><br><span class="line">github-light</span><br><span class="line">github-dark</span><br><span class="line">github-dark-orange</span><br><span class="line">icy-dark</span><br><span class="line">dark-blue</span><br><span class="line">photon-dark</span><br><span class="line">评论 issue-term 映射配置选项如下：</span><br><span class="line"></span><br><span class="line">pathname</span><br><span class="line">url</span><br><span class="line">title</span><br><span class="line">og:title</span><br><span class="line">issue-number</span><br><span class="line">specific-term</span><br><span class="line"></span><br><span class="line">vi thems\next\layout\_third-party\comments\utterance.swig</span><br><span class="line">&lt;script type=<span class="string">"text/javascript"</span>&gt;</span><br><span class="line">    (<span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">        <span class="comment">// 匿名函数，防止污染全局变量</span></span><br><span class="line">        <span class="keyword">var</span> utterances = <span class="built_in">document</span>.createElement(<span class="string">'script'</span>);</span><br><span class="line">        utterances.type = <span class="string">'text/javascript'</span>;</span><br><span class="line">        utterances.async = <span class="literal">true</span>;</span><br><span class="line">        utterances.setAttribute(<span class="string">'issue-term'</span>,<span class="string">'&#123;&#123; theme.utterance.issue-item &#125;&#125;'</span>)</span><br><span class="line">        utterances.setAttribute(<span class="string">'theme'</span>,<span class="string">'&#123;&#123; themm.utterance.theme &#125;&#125;'</span>)</span><br><span class="line">        utterances.setAttribute(<span class="string">'repo'</span>,<span class="string">'&#123;&#123; theme.utterance.repo &#125;&#125;'</span>)</span><br><span class="line">        utterances.crossorigin = <span class="string">'anonymous'</span>;</span><br><span class="line">        utterances.src = <span class="string">'https://utteranc.es/client.js'</span>;</span><br><span class="line">        <span class="comment">// content 是要插入评论的地方</span></span><br><span class="line">        <span class="built_in">document</span>.getElementById(<span class="string">'gitment-container'</span>).appendChild(utterances);</span><br><span class="line">    &#125;)();</span><br><span class="line">&lt;<span class="regexp">/script&gt;</span></span><br><span class="line"><span class="regexp">vi thems\next\layout\_partials\comments.swig</span></span><br><span class="line"><span class="regexp">&#123;% endif %&#125;之前添加</span></span><br><span class="line"><span class="regexp">&#123;% elif theme.utterance.enable %&#125;</span></span><br><span class="line"><span class="regexp">          &lt;div class="comments" id="comments"&gt;</span></span><br><span class="line"><span class="regexp">             &lt;div id="gitment-container"&gt;&lt;/</span>div&gt;</span><br><span class="line">         &lt;<span class="regexp">/div&gt;</span></span><br><span class="line"><span class="regexp"> vi thems\next\layout\_third-party\comments\index.swig  </span></span><br><span class="line"><span class="regexp">  &#123;% include 'utterance.swig' %&#125;        </span></span><br><span class="line"><span class="regexp">       </span></span><br><span class="line"><span class="regexp">  评论后内容在 https:/</span><span class="regexp">/github.com/</span>sushengbuhuo/laravel_ioc_demo/issues</span><br></pre></td></tr></table></figure>
<h3 id="备份到GitHub"><a href="#备份到GitHub" class="headerlink" title="备份到GitHub"></a>备份到GitHub</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line">https:<span class="comment">//github.com/coneycode/hexo-git-backup</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> your hexo version is <span class="number">2.</span>x.x, you should install <span class="keyword">as</span> follow:</span><br><span class="line"></span><br><span class="line">$ npm install hexo-git-backup@<span class="number">0.0</span><span class="number">.91</span> --save</span><br><span class="line"><span class="keyword">if</span> version is <span class="number">3.</span>x.x, you should install <span class="keyword">as</span> follow:</span><br><span class="line"></span><br><span class="line">$ npm install hexo-git-backup --save</span><br><span class="line">Update</span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> you install <span class="keyword">with</span> --save, you must remove firstly when you update it.</span><br><span class="line"></span><br><span class="line">$ npm remove hexo-git-backup</span><br><span class="line">$ npm install hexo-git-backup --save</span><br><span class="line">Configure</span><br><span class="line"></span><br><span class="line">You should configure <span class="keyword">this</span> plugin <span class="keyword">in</span> _config.yml.</span><br><span class="line"></span><br><span class="line">backup:</span><br><span class="line">    type: git</span><br><span class="line">    repository:</span><br><span class="line">       github: git@github.com:xxx/xxx.git,branchName</span><br><span class="line">       gitcafe: git@github.com:xxx/xxx.git,branchName</span><br><span class="line"> </span><br><span class="line"></span><br><span class="line">hexo b</span><br></pre></td></tr></table></figure>
<h3 id="微博图床失效问题"><a href="#微博图床失效问题" class="headerlink" title="微博图床失效问题"></a>微博图床失效问题</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">打开/themes/next/layout/_partials/head.swig文件,添加一行代码：&lt;meta name=<span class="string">"referrer"</span> content=<span class="string">"no-referrer"</span> /&gt;,具体如下所示：</span><br><span class="line">&lt;meta charset=<span class="string">"UTF-8"</span>/&gt;</span><br><span class="line">&lt;meta http-equiv=<span class="string">"X-UA-Compatible"</span> content=<span class="string">"IE=edge"</span> /&gt;</span><br><span class="line">&lt;meta name=<span class="string">"referrer"</span> content=<span class="string">"no-referrer"</span> /&gt;</span><br><span class="line">&lt;meta name=<span class="string">"viewport"</span> content=<span class="string">"width=device-width, initial-scale=1, maximum-scale=1"</span>/&gt;</span><br><span class="line">&lt;meta name=<span class="string">"theme-color"</span> content=<span class="string">"&#123;&#123; theme.android_chrome_color &#125;&#125;"</span>&gt;</span><br><span class="line">https:<span class="comment">//xuezheng-wei.com/posts/d970820/</span></span><br></pre></td></tr></table></figure>
<h3 id="Travis-CI-自动部署"><a href="#Travis-CI-自动部署" class="headerlink" title="Travis CI 自动部署"></a>Travis CI 自动部署</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br></pre></td><td class="code"><pre><span class="line">新建hexo分支</span><br><span class="line">添加 .travis.yml 在博客源文件根目录下并上传:</span><br><span class="line">language: node_js</span><br><span class="line">node_js: stable</span><br><span class="line"></span><br><span class="line">cache:</span><br><span class="line">  directories:</span><br><span class="line">    - node_modules</span><br><span class="line">before_install:</span><br><span class="line">  - npm install hexo-cli -g</span><br><span class="line"></span><br><span class="line">install:</span><br><span class="line">  - npm install</span><br><span class="line">  - npm install hexo-deployer-git --save</span><br><span class="line"></span><br><span class="line">script:</span><br><span class="line">  - hexo clean</span><br><span class="line">  - hexo generate</span><br><span class="line"></span><br><span class="line">after_script:</span><br><span class="line">  - cd ./public</span><br><span class="line">  - git init</span><br><span class="line">  - git config user.name <span class="string">"username"</span></span><br><span class="line">  - git config user.email <span class="string">"useremail"</span></span><br><span class="line">  - git add .</span><br><span class="line">  - git commit -m <span class="string">"Update docs"</span></span><br><span class="line">  - git push --force --quiet <span class="string">"https://$&#123;GH_TOKEN&#125;@$&#123;GH_REF&#125;"</span> master:master</span><br><span class="line"></span><br><span class="line">branches:</span><br><span class="line">  only:</span><br><span class="line">    - hexo</span><br><span class="line">env:</span><br><span class="line"> global:</span><br><span class="line">   - GH_REF: github.com/username/username.github.io.git</span><br><span class="line">   </span><br><span class="line"> （三）配置GitHub Access Token：</span><br><span class="line"> </span><br><span class="line"> GitHub 主页 ——&gt; Settings ——&gt; Developer Settings ——&gt; Personal access tokens ——&gt; Generate <span class="keyword">new</span> token</span><br><span class="line"> </span><br><span class="line"> （四）Travis CI 设置：</span><br><span class="line"> </span><br><span class="line">    （<span class="number">1</span>）登录 https:<span class="comment">//travis-ci.org/ 网站用 github 授权登录；</span></span><br><span class="line">    （<span class="number">2</span>）登录后在个人主页选择你需要 CI 的仓库；</span><br><span class="line">    （<span class="number">3</span>）点击你选择的 hexo 博客的仓库进行配置；</span><br><span class="line">    （<span class="number">4</span>）在 Travis 仓库配置界面 setting 里面对环境变量 Environment Variables 进行 token 配置；</span><br><span class="line"> </span><br><span class="line"> （五）撰写文章并 push 到 github pages：</span><br><span class="line"> </span><br><span class="line"> 每次写完文章，只需要执行下面的命令，其余部分会自动完成部署。https:<span class="comment">//xuezheng-wei.com/posts/8a945b8a/</span></span><br><span class="line"> git add .</span><br><span class="line"> git commit -m <span class="string">"updated docs"</span></span><br><span class="line"> git push origin hexo</span><br></pre></td></tr></table></figure>
<h3 id="博客备份"><a href="#博客备份" class="headerlink" title="博客备份"></a>博客备份</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line">$ git init <span class="comment">//git初始化</span></span><br><span class="line">$ git add . <span class="comment">//git 文件添加</span></span><br><span class="line">$ git commit -m <span class="string">"init"</span> <span class="comment">//git 提交</span></span><br><span class="line">$ git pull origin hexo <span class="comment">//pull到hexo分支</span></span><br><span class="line">$ git push origin hexo <span class="comment">//push到hexo分支</span></span><br><span class="line">博客恢复</span><br><span class="line">（一）配置 ssh 连接 Github</span><br><span class="line">$ cd ~<span class="regexp">/.ssh 或cd .ssh /</span><span class="regexp">/检查本机是否有ssh key设置</span></span><br><span class="line"><span class="regexp">$ cd ~  /</span><span class="regexp">/若没有 ssh ，则切换当前路径在 ”~” 下</span></span><br><span class="line"><span class="regexp">$ ssh-keygen -t rsa -C "user@example.com" /</span><span class="regexp">/引号内为自己邮箱，三个回车后生成ssh key;添加id_rsa.pub内容到Github;</span></span><br><span class="line"><span class="regexp">$ git config --global user.name “your_username”  /</span><span class="regexp">/设置用户名</span></span><br><span class="line"><span class="regexp">$ git config --global user.email “your_registered_github_Email”  /</span><span class="regexp">/设置邮箱地址(建议用注册giuhub的邮箱)</span></span><br><span class="line"><span class="regexp">$ ssh -T git@github.com /</span><span class="regexp">/测试ssh key是否设置成功</span></span><br><span class="line"><span class="regexp">（二）安装 Node.js；Git；Hexo；</span></span><br><span class="line"><span class="regexp">$ git clone -b hexo git@github.com:user/u</span>ser.github.io.git  <span class="comment">//将Github中hexo分支clone到本地</span></span><br><span class="line">$ cd user.github.io <span class="comment">//切换到hexo目录下</span></span><br><span class="line">$ npm install hexo</span><br><span class="line">$ npm install </span><br><span class="line">$ npm install hexo *** <span class="comment">//安装需要的插件：feed;deployer;abbrlink;sitemap;pdf;nofollow;baidu-url-submit等</span></span><br><span class="line">$ hexo g -d <span class="comment">//测试能否正常编译上传</span></span><br><span class="line">https:<span class="comment">//xuezheng-wei.com/posts/45a0debd/</span></span><br></pre></td></tr></table></figure>
<h3 id="图片防盗链"><a href="#图片防盗链" class="headerlink" title="图片防盗链"></a>图片防盗链</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">&lt;meta name=<span class="string">"referrer"</span> content=<span class="string">"never"</span> /&gt; ​​​​</span><br><span class="line">Next 主题只需要在 themes/next/layout/_partials/head/custom-head.swig 中添加上面一行内容即可</span><br><span class="line">https:<span class="comment">//michael728.github.io/2019/05/19/hexo-blog-full-note/</span></span><br></pre></td></tr></table></figure>
<h3 id="文章加密"><a href="#文章加密" class="headerlink" title="文章加密"></a>文章加密</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line">打开themes-&gt;next-&gt;layout-&gt;_partials-&gt;head.swig文件,在以下位置插入这样一段代码：</span><br><span class="line">&lt;script&gt;</span><br><span class="line">    (<span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">        <span class="keyword">if</span> (<span class="string">'&#123;&#123; page.password &#125;&#125;'</span>) &#123;</span><br><span class="line">            <span class="keyword">if</span> (prompt(<span class="string">'请输入文章密码'</span>) !== <span class="string">'&#123;&#123; page.password &#125;&#125;'</span>) &#123;</span><br><span class="line">                alert(<span class="string">'密码错误！'</span>);</span><br><span class="line">                <span class="keyword">if</span> (history.length === <span class="number">1</span>) &#123;</span><br><span class="line">                    location.replace(<span class="string">"http://xxxxxxx.xxx"</span>); <span class="comment">// 这里替换成你的首页</span></span><br><span class="line">                &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                    history.back();</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;)();</span><br><span class="line">&lt;<span class="regexp">/script&gt;</span></span><br><span class="line"><span class="regexp"></span></span><br><span class="line"><span class="regexp">在文章上写成类似这样</span></span><br><span class="line"><span class="regexp">---</span></span><br><span class="line"><span class="regexp">title: &#123;&#123; title &#125;&#125;</span></span><br><span class="line"><span class="regexp">date: &#123;&#123; date &#125;&#125;</span></span><br><span class="line"><span class="regexp">tags:</span></span><br><span class="line"><span class="regexp">description: </span></span><br><span class="line"><span class="regexp">copyright: </span></span><br><span class="line"><span class="regexp">categories:</span></span><br><span class="line"><span class="regexp">password: password</span></span><br><span class="line"><span class="regexp">---</span></span><br><span class="line"><span class="regexp"></span></span><br><span class="line"><span class="regexp">但是通过curl工具浏览网页是直接显示网页源代码就可以看到密码</span></span><br><span class="line"><span class="regexp">curl -L http:/</span><span class="regexp">/</span></span><br><span class="line"><span class="regexp">可以使用https:/</span><span class="regexp">/github.com/</span>MikeCoder/hexo-blog-encrypt/blob/master/ReadMe.zh.md 这个插件加密</span><br><span class="line">https:<span class="comment">//www.jianshu.com/p/44e211829447</span></span><br></pre></td></tr></table></figure>
<h3 id="推送到gitee"><a href="#推送到gitee" class="headerlink" title="推送到gitee"></a>推送到gitee</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br></pre></td><td class="code"><pre><span class="line">创建项目 https:<span class="comment">//gitee.com/sushengbuhuo/sushengbuhuo  注意和GitHub不一样，项目名不是sushengbuhuo.gitee.io</span></span><br><span class="line">vi _config.yml</span><br><span class="line"></span><br><span class="line">deploy:</span><br><span class="line">  type: git</span><br><span class="line">  repository:</span><br><span class="line">    gitee: git@gitee.com:sushengbuhuo/sushengbuhuo.git</span><br><span class="line">    github: git@sushengbuhuo.github.com:sushengbuhuo/sushengbuhuo.github.io.git</span><br><span class="line">  branch: master</span><br><span class="line"></span><br><span class="line">hexo d </span><br><span class="line"></span><br><span class="line">[master f11b6ad] Site updated: <span class="number">2019</span><span class="number">-10</span><span class="number">-11</span> <span class="number">16</span>:<span class="number">37</span>:<span class="number">34</span></span><br><span class="line"> <span class="number">235</span> files changed, <span class="number">248</span> insertions(+), <span class="number">246</span> deletions(-)</span><br><span class="line">remote: Powered By Gitee.com</span><br><span class="line">Branch master set up to track remote branch master <span class="keyword">from</span> git@gitee.com:sushengbuhuo/sushengbuhuo.git.</span><br><span class="line">To gitee.com:sushengbuhuo/sushengbuhuo.git</span><br><span class="line"> + <span class="number">538</span>b9d3...f11b6ad HEAD -&gt; master (forced update)</span><br><span class="line">On branch master</span><br><span class="line"></span><br><span class="line">部署完后需要https:<span class="comment">//gitee.com/sushengbuhuo/sushengbuhuo/pages 自己手动更新部署。</span></span><br><span class="line"></span><br><span class="line">访问  https:<span class="comment">//sushengbuhuo.gitee.io</span></span><br><span class="line"></span><br><span class="line">git checkout -b hexo</span><br><span class="line">λ git branch</span><br><span class="line">  hexo</span><br><span class="line">* master</span><br><span class="line"></span><br><span class="line">d:\code\hexo\blog (master)</span><br><span class="line">λ git remote add gitee git@gitee.com:sushengbuhuo/sushengbuhuo.git</span><br><span class="line"></span><br><span class="line">d:\code\hexo\blog (master)</span><br><span class="line">λ git remote -v</span><br><span class="line">gitee   git@gitee.com:sushengbuhuo/sushengbuhuo.git (fetch)</span><br><span class="line">gitee   git@gitee.com:sushengbuhuo/sushengbuhuo.git (push)</span><br><span class="line">github  git@sushengbuhuo.github.com:sushengbuhuo/sushengbuhuo.github.io.git (fetch)</span><br><span class="line">github  git@sushengbuhuo.github.com:sushengbuhuo/sushengbuhuo.github.io.git (push)</span><br><span class="line">λ git push gitee hexo</span><br><span class="line">Counting objects: <span class="number">580</span>, done.</span><br><span class="line">Delta compression using up to <span class="number">4</span> threads.</span><br><span class="line">Compressing objects: <span class="number">100</span>% (<span class="number">541</span>/<span class="number">541</span>), done.</span><br><span class="line">Writing objects: <span class="number">100</span>% (<span class="number">580</span>/<span class="number">580</span>), <span class="number">2.21</span> MiB | <span class="number">2.77</span> MiB/s, done.</span><br><span class="line">Total <span class="number">580</span> (delta <span class="number">33</span>), reused <span class="number">0</span> (delta <span class="number">0</span>)</span><br><span class="line">remote: Resolving deltas: <span class="number">100</span>% (<span class="number">33</span>/<span class="number">33</span>), done.</span><br><span class="line">remote: Powered By Gitee.com</span><br><span class="line">To gitee.com:sushengbuhuo/sushengbuhuo.git</span><br><span class="line"> * [<span class="keyword">new</span> branch]      hexo -&gt; hexo</span><br></pre></td></tr></table></figure>
<p><a href="https://hexo.io/zh-cn/docs/writing.html" target="_blank" rel="noopener">新写文章文档</a></p>
<h3 id="资源"><a href="#资源" class="headerlink" title="资源"></a>资源</h3><p><a href="https://github.com/8090Lambert/hexo-theme-easy" target="_blank" rel="noopener">至简的 hexo 主题</a></p>
<p><a href="https://www.mspring.org/2018/11/29/HexoClient%E4%BD%BF%E7%94%A8%E5%B8%AE%E5%8A%A9/" target="_blank" rel="noopener">开源的工具 HexoClient （跨平台的哦）</a></p>
<p><a href="https://duanruilong.github.io/blog/2018/05/05/next%E4%B8%BB%E9%A2%98%E6%A8%A1%E6%9D%BF%E4%B8%80%E4%BA%9B%E9%85%8D%E7%BD%AE/" target="_blank" rel="noopener">next主题模板一些配置</a></p>
<p><a href="http://www.arao.me/2015/hexo-next-theme-optimize-base/" target="_blank" rel="noopener">动动手指，NexT主题与Hexo更搭哦（基础篇）</a></p>
<p><a href="https://www.njphper.com/posts/a4cd94b2.html" target="_blank" rel="noopener">Next 主题增加新的第三方评论系统 utterance</a></p>
<p><a href="https://juejin.im/post/5b14b2f06fb9a01e5e3d3121" target="_blank" rel="noopener">Github Pages部署教程</a></p>
<p><a href="https://www.playpi.org/2018112701.html" target="_blank" rel="noopener">GitHub.io 个人站点绑定独立的域名</a></p>
<p><a href="https://reuixiy.github.io/technology/computer/computer-aided-art/2017/06/09/hexo-next-optimization.html#附上我的-custom-styl" target="_blank" rel="noopener">打造个性超赞博客Hexo+NexT+GitHubPages的超深度优化</a></p>
<p><a href="https://www.makcyun.top/hexo02.html" target="_blank" rel="noopener">用Github+Hexo搭建你的个人博客：美化篇</a></p>
<p><a href="http://tengj.top/2016/02/20/hexoTotal/" target="_blank" rel="noopener">hexo干货系列</a></p>
<p><a href="https://hexo.io/docs/" target="_blank" rel="noopener">hexo文档</a></p>
<p><a href="https://hexo.io/docs/troubleshooting.html" target="_blank" rel="noopener">hexo问题交流</a> </p>
<p><a href="https://hexo.io/docs/deployment.html" target="_blank" rel="noopener">Deployment</a></p>
<p><a href="https://www.jianshu.com/p/21c94eb7bcd1" target="_blank" rel="noopener">绝配：hexo+next主题及我走过的坑</a></p>
<p><a href="https://blog.csdn.net/mango_haoming/article/details/78207534" target="_blank" rel="noopener">Hexo+Pages静态博客-Next主题篇</a></p>
<p><a href="https://laravel-china.org/articles/20991" target="_blank" rel="noopener">hexo部署到服务器</a></p>
<p><a href="https://juejin.im/post/5a6ee00ef265da3e4b770ac1#heading-6" target="_blank" rel="noopener">关于HEXO搭建个人博客的点点滴滴</a></p>
<p><a href="https://alvabill.ml/hexo%E6%90%AD%E5%BB%BA%E4%B8%AA%E4%BA%BA%E5%8D%9A%E5%AE%A2--%E5%9F%BA%E7%A1%80%E7%AF%87/" target="_blank" rel="noopener">搭建博客</a></p>
<p><a href="https://juejin.im/post/5abcd2286fb9a028d66440ba" target="_blank" rel="noopener">手把手教你用Hexo搭建个人技术博客</a></p>
<p><a href="https://segmentfault.com/a/1190000012907499" target="_blank" rel="noopener">基于CentOS搭建Hexo博客</a></p>
<p><a href="https://blog.coding.net/blog/CS-Hexo" target="_blank" rel="noopener">最快的 Hexo 博客搭建方法</a></p>
<p><a href="https://zhuanlan.zhihu.com/p/35668237" target="_blank" rel="noopener">超详细Hexo+Github博客搭建小白教程</a></p>
<p><a href="https://lrscy.github.io/2018/01/26/Hexo-Github-Backup/" target="_blank" rel="noopener">在Github上备份Hexo博客</a></p>
<p><a href="https://blog.coding.net/blog/CS-Hexo" target="_blank" rel="noopener">最快的 Hexo 博客搭建方法</a></p>
<p><a href="http://www.gohugo.org/" target="_blank" rel="noopener">Hugo是由Go语言实现的静态网站生成器</a></p>
<p><a href="http://linbinghe.com/2017/982b12f0.html" target="_blank" rel="noopener">带点Geek味的独立博客</a></p>
<p><a href="https://blog.mutoe.com/2017/deploy-hexo-website-to-self-server/" target="_blank" rel="noopener">将 Hexo 博客发布到自己的服务器上</a></p>
<p><a href="https://github.com/litten/hexo-theme-yilia" target="_blank" rel="noopener">yilia主题</a></p>
<p><a href="https://segmentfault.com/a/1190000012975914#articleHeader7" target="_blank" rel="noopener">使用 Hugo 搭建博客</a></p>
<p><a href="https://www.fanhaobai.com/2018/03/hexo-deploy.html" target="_blank" rel="noopener">博客发布上线方案 — Hexo</a></p>
<p><a href="https://github.com/fan-haobai/blog" target="_blank" rel="noopener">hexo博客</a></p>
<p><a href="http://coderlt.coding.me/2015/09/21/blog-with-hexo/" target="_blank" rel="noopener">使用HEXO搭建博客</a></p>
<p><a href="http://coderlt.coding.me/2015/09/24/hexo-to-coding-150924/" target="_blank" rel="noopener">部署Hexo博客到Coding</a></p>
<p><a href="https://www.makcyun.top/hexo02.html" target="_blank" rel="noopener">用Github+Hexo搭建你的个人博客：美化篇</a></p>
<p><a href="https://valine.js.org/" target="_blank" rel="noopener">无后端评论系统</a></p>
<p><a href="https://yfzhou.coding.me/categories/Hexo/" target="_blank" rel="noopener">hexo博客专题</a></p>
<p><a href="https://swoole.app/2016/07/23/hexo%E5%8D%9A%E5%AE%A2%E6%B7%BB%E5%8A%A0%E6%A0%87%E7%AD%BE%E5%8A%9F%E8%83%BD/" target="_blank" rel="noopener">hexo博客添加标签功能</a></p>
<p><a href="https://www.njphper.com/posts/a4cd94b2.html" target="_blank" rel="noopener">第三方评论系统 utterance</a></p>
<p><a href="https://michael728.github.io/2019/05/19/hexo-blog-full-note/" target="_blank" rel="noopener"> Hexo + Github Pages 搭建博客并优化 Next 主题教程</a></p>
<p><a href="https://live2d.fghrsh.net/demo/1.4.2/autoload.html" target="_blank" rel="noopener">博客看板娘</a></p>

          
        
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
  </section>

  
  <nav class="pagination">
    <a class="extend prev" rel="prev" href="/page/38/"><i class="fa fa-angle-left"></i></a><a class="page-number" href="/">1</a><span class="space">&hellip;</span><a class="page-number" href="/page/38/">38</a><span class="page-number current">39</span>
  </nav>



          </div>
          


          

        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      

      <section class="site-overview-wrap sidebar-panel sidebar-panel-active">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope="" itemtype="http://schema.org/Person">
            
              <p class="site-author-name" itemprop="name">苏生不惑</p>
              <p class="site-description motion-element" itemprop="description"></p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives/">
              
                  <span class="site-state-item-count">382</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            

            
              
              
              <div class="site-state-item site-state-tags">
                <a href="/tags/index.html">
                  <span class="site-state-item-count">37</span>
                  <span class="site-state-item-name">标签</span>
                </a>
              </div>
            

          </nav>

          

          
            <div class="links-of-author motion-element">
                
                  <span class="links-of-author-item">
                    <a href="https://github.com/sushengbuhuo" target="_blank" title="GitHub">
                      
                        <i class="fa fa-fw fa-github"></i>GitHub</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="mysusheng@gmail.com" target="_blank" title="E-Mail">
                      
                        <i class="fa fa-fw fa-envelope"></i>E-Mail</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://plus.google.com/mysusheng" target="_blank" title="Google">
                      
                        <i class="fa fa-fw fa-google"></i>Google</a>
                  </span>
                
            </div>
          

          
          

          
          
            <div class="links-of-blogroll motion-element links-of-blogroll-block">
              <div class="links-of-blogroll-title">
                <i class="fa  fa-fw fa-link"></i>
                Links
              </div>
              <ul class="links-of-blogroll-list">
                
                  <li class="links-of-blogroll-item">
                    <a href="http://v2ex.com/" title="v2ex" target="_blank">v2ex</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.fanhaobai.com/" title="fanhaobai" target="_blank">fanhaobai</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://yuanxuxu.com/archives/" title="yuanxuxu" target="_blank">yuanxuxu</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.snail-c.cn/article" title="snail-c" target="_blank">snail-c</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="http://showcj.com/archives" title="showcj" target="_blank">showcj</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="http://vultr.aicnm.com/%E6%9C%80%E6%96%B0Vultr%E6%B3%A8%E5%86%8C%E5%8F%8AVPS%E8%B4%AD%E4%B9%B0%E5%9B%BE%E6%96%87%E6%95%99%E7%A8%8B/" title="vultr" target="_blank">vultr</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.lucissfer.com/" title="lucissfer" target="_blank">lucissfer</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://blog.csdn.net/fdipzone/article/details/79352685" title="傲雪星枫" target="_blank">傲雪星枫</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.yoby123.cn/index.php/category/default/" title="小白的分享" target="_blank">小白的分享</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.cnblogs.com/52fhy/p/5819995.html" title="PHP攻城狮" target="_blank">PHP攻城狮</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://blog.jiaojie.site/" title="php" target="_blank">php</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://sphard.com/archives/" title="sphard" target="_blank">sphard</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://yuanxuxu.com/archives/" title="LNMP技术栈笔记" target="_blank">LNMP技术栈笔记</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="http://www.coding10.com/" title="学习 Laravel" target="_blank">学习 Laravel</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://shuwoom.com/?page_id=929" title="区块链学习指南" target="_blank">区块链学习指南</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://greenlightt.github.io/archives/" title="greenlightt" target="_blank">greenlightt</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.0php.net/archives/" title="0php" target="_blank">0php</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="http://www.fordba.com/category/mysql" title="mysql" target="_blank">mysql</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.itcodemonkey.com/" title="程序员" target="_blank">程序员</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.yanshuo.me/r/v2ex" title="言说" target="_blank">言说</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.timiguo.com/archive.html" title="提米果的博客" target="_blank">提米果的博客</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://phpartisan.cn/news/112.html" title="phpartisan" target="_blank">phpartisan</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.cnblogs.com/52fhy/" title="飞鸿影" target="_blank">飞鸿影</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="http://www.54php.cn/" title="54php" target="_blank">54php</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.lazyman.vip/" title="营销" target="_blank">营销</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.njphper.com/archives/" title="做人呢最重要的就是开心" target="_blank">做人呢最重要的就是开心</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.h57.pw/" title="php 初心者" target="_blank">php 初心者</a>
                  </li>
                
              </ul>
            </div>
          

          

        </div>
      </section>

      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <script async src="https://dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js"></script>

<div class="copyright">&copy; <span itemprop="copyrightYear">2021</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">苏生不惑</span>

  
</div>


  <div class="powered-by">由 <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a> 强力驱动</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Gemini</a> v5.1.4</div>



<div>
<span id="showDays"></span>

</div>

<span id="busuanzi_container_site_pv">
   总访问量:<span id="busuanzi_value_site_pv"></span>次
</span>



  <span class="post-meta-divider">|</span>



<div class="theme-info">
  <div class="powered-by"></div>
  <span class="post-count">博客全站共773.8k字</span>
</div>
<script>
var birthDay = new Date("11/20/2018");
var now = new Date();
var duration = now.getTime() - birthDay.getTime();
var total= Math.floor(duration / (1000 * 60 * 60 * 24));
document.getElementById("showDays").innerHTML = "本站已运行 "+total+" 天";
</script>

        
<div class="busuanzi-count">
  <script async src="https://dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js"></script>

  
    <span class="site-uv">
      <i class="fa fa-user"></i>
      <span class="busuanzi-value" id="busuanzi_value_site_uv"></span>
      人次
    </span>
  

  
    <span class="site-pv">
      <i class="fa fa-eye"></i>
      <span class="busuanzi-value" id="busuanzi_value_site_pv"></span>
      次
    </span>
  
</div>








        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  


  











  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  

  
  
    <script type="text/javascript" src="/lib/canvas-nest/canvas-nest.min.js"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.4"></script>



  
  


  <script type="text/javascript" src="/js/src/affix.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/schemes/pisces.js?v=5.1.4"></script>



  

  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  











<script type="text/javascript">
    (function() {
        // 匿名函数，防止污染全局变量
        var utterances = document.createElement('script');
        utterances.type = 'text/javascript';
        utterances.async = true;
        utterances.setAttribute('issue-term','0')
        utterances.setAttribute('theme','')
        utterances.setAttribute('repo','sushengbuhuo/laravel_ioc_demo')
        utterances.crossorigin = 'anonymous';
        utterances.src = 'https://utteranc.es/client.js';
        // content 是要插入评论的地方
        document.getElementById('gitment-container').appendChild(utterances);
    })();
</script>


  

  <script type="text/javascript">
    // Popup Window;
    var isfetched = false;
    var isXml = true;
    // Search DB path;
    var search_path = "search.xml";
    if (search_path.length === 0) {
      search_path = "search.xml";
    } else if (/json$/i.test(search_path)) {
      isXml = false;
    }
    var path = "/" + search_path;
    // monitor main search box;

    var onPopupClose = function (e) {
      $('.popup').hide();
      $('#local-search-input').val('');
      $('.search-result-list').remove();
      $('#no-result').remove();
      $(".local-search-pop-overlay").remove();
      $('body').css('overflow', '');
    }

    function proceedsearch() {
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay"></div>')
        .css('overflow', 'hidden');
      $('.search-popup-overlay').click(onPopupClose);
      $('.popup').toggle();
      var $localSearchInput = $('#local-search-input');
      $localSearchInput.attr("autocapitalize", "none");
      $localSearchInput.attr("autocorrect", "off");
      $localSearchInput.focus();
    }

    // search function;
    var searchFunc = function(path, search_id, content_id) {
      'use strict';

      // start loading animation
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay">' +
          '<div id="search-loading-icon">' +
          '<i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>' +
          '</div>' +
          '</div>')
        .css('overflow', 'hidden');
      $("#search-loading-icon").css('margin', '20% auto 0 auto').css('text-align', 'center');

      $.ajax({
        url: path,
        dataType: isXml ? "xml" : "json",
        async: true,
        success: function(res) {
          // get the contents from search data
          isfetched = true;
          $('.popup').detach().appendTo('.header-inner');
          var datas = isXml ? $("entry", res).map(function() {
            return {
              title: $("title", this).text(),
              content: $("content",this).text(),
              url: $("url" , this).text()
            };
          }).get() : res;
          var input = document.getElementById(search_id);
          var resultContent = document.getElementById(content_id);
          var inputEventFunction = function() {
            var searchText = input.value.trim().toLowerCase();
            var keywords = searchText.split(/[\s\-]+/);
            if (keywords.length > 1) {
              keywords.push(searchText);
            }
            var resultItems = [];
            if (searchText.length > 0) {
              // perform local searching
              datas.forEach(function(data) {
                var isMatch = false;
                var hitCount = 0;
                var searchTextCount = 0;
                var title = data.title.trim();
                var titleInLowerCase = title.toLowerCase();
                var content = data.content.trim().replace(/<[^>]+>/g,"");
                var contentInLowerCase = content.toLowerCase();
                var articleUrl = decodeURIComponent(data.url);
                var indexOfTitle = [];
                var indexOfContent = [];
                // only match articles with not empty titles
                if(title != '') {
                  keywords.forEach(function(keyword) {
                    function getIndexByWord(word, text, caseSensitive) {
                      var wordLen = word.length;
                      if (wordLen === 0) {
                        return [];
                      }
                      var startPosition = 0, position = [], index = [];
                      if (!caseSensitive) {
                        text = text.toLowerCase();
                        word = word.toLowerCase();
                      }
                      while ((position = text.indexOf(word, startPosition)) > -1) {
                        index.push({position: position, word: word});
                        startPosition = position + wordLen;
                      }
                      return index;
                    }

                    indexOfTitle = indexOfTitle.concat(getIndexByWord(keyword, titleInLowerCase, false));
                    indexOfContent = indexOfContent.concat(getIndexByWord(keyword, contentInLowerCase, false));
                  });
                  if (indexOfTitle.length > 0 || indexOfContent.length > 0) {
                    isMatch = true;
                    hitCount = indexOfTitle.length + indexOfContent.length;
                  }
                }

                // show search results

                if (isMatch) {
                  // sort index by position of keyword

                  [indexOfTitle, indexOfContent].forEach(function (index) {
                    index.sort(function (itemLeft, itemRight) {
                      if (itemRight.position !== itemLeft.position) {
                        return itemRight.position - itemLeft.position;
                      } else {
                        return itemLeft.word.length - itemRight.word.length;
                      }
                    });
                  });

                  // merge hits into slices

                  function mergeIntoSlice(text, start, end, index) {
                    var item = index[index.length - 1];
                    var position = item.position;
                    var word = item.word;
                    var hits = [];
                    var searchTextCountInSlice = 0;
                    while (position + word.length <= end && index.length != 0) {
                      if (word === searchText) {
                        searchTextCountInSlice++;
                      }
                      hits.push({position: position, length: word.length});
                      var wordEnd = position + word.length;

                      // move to next position of hit

                      index.pop();
                      while (index.length != 0) {
                        item = index[index.length - 1];
                        position = item.position;
                        word = item.word;
                        if (wordEnd > position) {
                          index.pop();
                        } else {
                          break;
                        }
                      }
                    }
                    searchTextCount += searchTextCountInSlice;
                    return {
                      hits: hits,
                      start: start,
                      end: end,
                      searchTextCount: searchTextCountInSlice
                    };
                  }

                  var slicesOfTitle = [];
                  if (indexOfTitle.length != 0) {
                    slicesOfTitle.push(mergeIntoSlice(title, 0, title.length, indexOfTitle));
                  }

                  var slicesOfContent = [];
                  while (indexOfContent.length != 0) {
                    var item = indexOfContent[indexOfContent.length - 1];
                    var position = item.position;
                    var word = item.word;
                    // cut out 100 characters
                    var start = position - 20;
                    var end = position + 80;
                    if(start < 0){
                      start = 0;
                    }
                    if (end < position + word.length) {
                      end = position + word.length;
                    }
                    if(end > content.length){
                      end = content.length;
                    }
                    slicesOfContent.push(mergeIntoSlice(content, start, end, indexOfContent));
                  }

                  // sort slices in content by search text's count and hits' count

                  slicesOfContent.sort(function (sliceLeft, sliceRight) {
                    if (sliceLeft.searchTextCount !== sliceRight.searchTextCount) {
                      return sliceRight.searchTextCount - sliceLeft.searchTextCount;
                    } else if (sliceLeft.hits.length !== sliceRight.hits.length) {
                      return sliceRight.hits.length - sliceLeft.hits.length;
                    } else {
                      return sliceLeft.start - sliceRight.start;
                    }
                  });

                  // select top N slices in content

                  var upperBound = parseInt('1');
                  if (upperBound >= 0) {
                    slicesOfContent = slicesOfContent.slice(0, upperBound);
                  }

                  // highlight title and content

                  function highlightKeyword(text, slice) {
                    var result = '';
                    var prevEnd = slice.start;
                    slice.hits.forEach(function (hit) {
                      result += text.substring(prevEnd, hit.position);
                      var end = hit.position + hit.length;
                      result += '<b class="search-keyword">' + text.substring(hit.position, end) + '</b>';
                      prevEnd = end;
                    });
                    result += text.substring(prevEnd, slice.end);
                    return result;
                  }

                  var resultItem = '';

                  if (slicesOfTitle.length != 0) {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + highlightKeyword(title, slicesOfTitle[0]) + "</a>";
                  } else {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + title + "</a>";
                  }

                  slicesOfContent.forEach(function (slice) {
                    resultItem += "<a href='" + articleUrl + "'>" +
                      "<p class=\"search-result\">" + highlightKeyword(content, slice) +
                      "...</p>" + "</a>";
                  });

                  resultItem += "</li>";
                  resultItems.push({
                    item: resultItem,
                    searchTextCount: searchTextCount,
                    hitCount: hitCount,
                    id: resultItems.length
                  });
                }
              })
            };
            if (keywords.length === 1 && keywords[0] === "") {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-search fa-5x" /></div>'
            } else if (resultItems.length === 0) {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-frown-o fa-5x" /></div>'
            } else {
              resultItems.sort(function (resultLeft, resultRight) {
                if (resultLeft.searchTextCount !== resultRight.searchTextCount) {
                  return resultRight.searchTextCount - resultLeft.searchTextCount;
                } else if (resultLeft.hitCount !== resultRight.hitCount) {
                  return resultRight.hitCount - resultLeft.hitCount;
                } else {
                  return resultRight.id - resultLeft.id;
                }
              });
              var searchResultList = '<ul class=\"search-result-list\">';
              resultItems.forEach(function (result) {
                searchResultList += result.item;
              })
              searchResultList += "</ul>";
              resultContent.innerHTML = searchResultList;
            }
          }

          if ('auto' === 'auto') {
            input.addEventListener('input', inputEventFunction);
          } else {
            $('.search-icon').click(inputEventFunction);
            input.addEventListener('keypress', function (event) {
              if (event.keyCode === 13) {
                inputEventFunction();
              }
            });
          }

          // remove loading animation
          $(".local-search-pop-overlay").remove();
          $('body').css('overflow', '');

          proceedsearch();
        }
      });
    }

    // handle and trigger popup window;
    $('.popup-trigger').click(function(e) {
      e.stopPropagation();
      if (isfetched === false) {
        searchFunc(path, 'local-search-input', 'local-search-result');
      } else {
        proceedsearch();
      };
    });

    $('.popup-btn-close').click(onPopupClose);
    $('.popup').click(function(e){
      e.stopPropagation();
    });
    $(document).on('keyup', function (event) {
      var shouldDismissSearchPopup = event.which === 27 &&
        $('.search-popup').is(':visible');
      if (shouldDismissSearchPopup) {
        onPopupClose();
      }
    });
  </script>





  

  

  

  
  

  

  

  

  
<script>
  ((window.gitter = {}).chat = {}).options = {
    //room替换成自己的聊天室名称即可，room的名称规则是：username/roomname
    room: 'sushengbuhuo-chat/mychat'
  };
</script>
<script src="https://sidecar.gitter.im/dist/sidecar.v1.js" async defer></script>

</body>
</html>
